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);
请给我你的想法!谢谢你!!
For the life of me, I can't get the cursor to return any data. I've verified that their is data in the database and my insertions are working. The official error is:
CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
Top level declarations:
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
this.db = this.DBHelper.getWritableDatabase();
}
My function:
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);
}
I've also tried grabbing the cursor as such:
Cursor cursor = db.rawQuery(
"SELECT * FROM " + DATABASE_TABLE
+ " WHERE " + COLUMN_A + " = " + 0, null);
Please give me your thoughts! Thank you!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里似乎没有什么是随机的。看起来您每次都在寻找值为 0 的 column_a 。
我假设 column_a 没有任何值为 0 的内容。
Nothing seems random here at all. It appears that you are looking for column_a with a value of 0 every time.
I would assume that column_a has nothing with a value of 0.
首先尝试以这种方式查询:
如果您查询返回行,这不能帮助使用外部工具检查您的数据库。
First of all try to query this way:
If this not helps check your db with external tool if you query return rows there.