同じデータ (Double 型の配列) を SOP (sys.out.println) の同じ db/table に挿入すると、設定していないのに、なぜ「null(s)」が発生するのか疑問に思っていました。制約。
テーブルに「null」が入るのではないかと疑っていますが、それはどのように可能ですか? 初めて値を挿入するときは正常に動作しますが、同じ値を再度挿入しようとするとすぐに (アプリを 2 回実行すると)、logcat で SOP の出力が次のように取得されます。
12-14 13:24:43.788: I/System.out(763): 28.0
12-14 13:24:43.788: I/System.out(763): null
12-14 13:24:43.797: I/System.out(763): The data was added successfully!
12-14 13:24:43.797: I/System.out(763): Data Totals = 2
私の DBAdapter.java は次のようになります。
package com.profourth.line;
import java.util.Random;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
int id = 0;
public static final String KEY_PHID = "phID";
public String KEY_LAT = "LATS";
public String KEY_LON = "LONS";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "PROdb";
private static final String DATABASE_TABLE = "myPROmapData";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE = "create table myPROmapData (phID integer, LATS real, LONS real);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS myPROmapData");
onCreate(db);
}
}
// ---opens the database---
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close() {
DBHelper.close();
}
// ---insert a title into the database---
public long insertQuote(/* String Quote */) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_PHID, 100);
initialValues.put(KEY_LAT, 28.00);
initialValues.put(KEY_LON, 72.00);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public int getAllEntries() {
Cursor cursor = db.rawQuery("SELECT COUNT(LATS) FROM myPROmapData",
null);
if (cursor.moveToFirst()) {
return cursor.getInt(0);
}
return cursor.getInt(0);
}
public Double[] Populate() {
Double[] myLats;
Double[] myLons;
id = getAllEntries();
/*
* Random random = new Random(); int rand =
* random.nextInt(getAllEntries()); if(rand == 0) ++rand;
*/
Cursor cursor = db.rawQuery("SELECT LATS FROM myPROmapData", null);
/*
* if(cursor.moveToFirst()) { //return cursor.getString(0); } //return
* cursor.getString(0); return myLats;
*/
Double data[] = new Double[cursor.getCount()];
if (cursor != null) {
while (cursor.moveToNext()) {
int j = 0;
while (j < cursor.getColumnCount()) {
// data[j] = cursor.getString(j);
data[j] = cursor.getDouble(j);
j++;
}
cursor.moveToNext();
}
cursor.close();
}
return data;
}
}
そして私の活動は次のようになります:
package com.profourth.line;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.widget.Toast;
public class MapDataController extends Activity {
DBAdapter db = new DBAdapter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_data_controller);
/*MapDataHandler dbh = new MapDataHandler(this);
SQLiteDatabase db = dbh.getWritableDatabase();
MapDataHandler handlemydata = new MapDataHandler(null);
handlemydata.onCreate(db);
handlemydata.AddRows();
handlemydata.PopulateArr();
db.close();*/
db.open();
long id = 0;
db.insertQuote();
id = db.getAllEntries();
Double[] dta = null;
dta = db.Populate();
for(int z=0;z<(dta.length);z++)
{
System.out.println(dta[z]);
}
//Context context = getApplicationContext();
CharSequence text = "The data was added successfully!\nData Totals = " + id;
/*int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();*/
System.out.println(text);
//Quote.setText("");
db.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_map_data_controller, menu);
return true;
}
}
どんな助けでも大歓迎です。読んでくれてありがとう..
解決済み:
これが私の更新された Populate() メソッドです:
public Double[] Populate() {
Double[] myLats;
Double[] myLons;
//Double[] data = null;
id = getAllEntries();
/*
* Random random = new Random(); int rand =
* random.nextInt(getAllEntries()); if(rand == 0) ++rand;
*/
int columnIndex = 0;
Cursor cursor = db.rawQuery("SELECT LATS FROM myPROmapData", null);
/*
* if(cursor.moveToFirst()) { //return cursor.getString(0); } //return
* cursor.getString(0); return myLats;
*/
Double data[] = new Double[cursor.getCount()];
if (cursor.moveToFirst())
{
for (int i = 0; i < cursor.getCount(); i++)
{
data[i] = cursor.getDouble(columnIndex);
cursor.moveToNext();
}
}
cursor.close();
return data;
}