對於我的生活,我無法獲得遊標返回任何數據。我已驗證他們是數據庫中的數據,並且我的插入正在工作。官方的錯誤是:Android SQLLiteDataBase遊標返回0行
CursorIndexOutOfBoundsException:指數0要求,大小爲0
頂級聲明:
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
this.db = this.DBHelper.getWritableDatabase();
}
我的功能:
public String getRandomEntry()
{
int rand;
Random random = new Random();
int numEntries = (int)this.getCount();
if(numEntries == 0)
return "ERROR: Database is empty.";
rand = random.nextInt(numEntries);
Cursor cursor = DBHelper.getWritableDatabase().rawQuery(
"SELECT * FROM " + DATABASE_TABLE
+ " WHERE " + COLUMN_A + " = " + 0, null);
Log.i("numEntries", Integer.toString(numEntries));
Log.i("rand", Integer.toString(rand));
Log.i("cursor", Integer.toString(cursor.getCount()));
cursor.moveToFirst();
return cursor.getString(0);
}
我也試過了抓住光標爲:
Cursor cursor = db.rawQuery(
"SELECT * FROM " + DATABASE_TABLE
+ " WHERE " + COLUMN_A + " = " + 0, null);
請給我你的想法!謝謝!!
+1在db ...中使用SQLiteOpenHelper類 – 2011-06-15 18:37:25
是COLUMN_A = 0? ...什麼是「蘭特」? – Stuck 2011-06-15 18:40:42
STRING COLUMN_A =「_id」;對不起,應該包括那個。 – RedLeader 2011-06-15 18:56:13