请帮忙!访问 SQLite for android 返回的游标内的数据时出错
我是一个android新手,一直在尝试SQLite和Android。我能够获取查询并使用游标结果,但我似乎无法让以下代码正常工作:
这是我的查询语句,其中 queryWhere = "MyObject="1"" 和 DATABASE_TABLE_TOQUERY = "FavoriteObjects"。在 SQLite 数据库查看器中查看时这些都没有问题,如果我使用 SQLite 数据库查看器执行查询,它也会返回正确的结果。
public long findObject(String[] keys, String[] values,String DATABASE_TABLE_TOQUERY){
try {
if(keys.length<0 || keys.length!=values.length)
System.exit(-1);
String queryWhere = keys[0]+"=\""+values[0]+"\"";
for(int i=1;i<keys.length;i++) {
queryWhere+=" AND "+keys[i]+"=\""+values[i]+"\"";
}
Cursor mCursor = mDb.query(true, DATABASE_TABLE_TOQUERY, null,
queryWhere, null, null, null, null, null);
if (mCursor != null && mCursor.getCount()>0)
return Long.parseLong(mCursor.getString(mCursor.getColumnIndex(KEY_ROWID)));
}catch (SQLException e) {
return -1;
}
return -1;
}
当我执行查询语句后得到结果时,Cursor mCursor 至少有一行,但是当我使用 mCursor.getString(X) 访问该行时,它会抛出一个奇怪的错误:
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
如果我检查 mCursor.getColumnIndex(KEY_ROWID),我得到 0 值(这是正确的)。有人有什么建议吗?
谢谢! 乔恩
I am an android novice and have been experimenting with SQLite and Android. I am able to get queries and use the cursor results, but I can't seem to get this the following code to work:
This is my Query statement, where queryWhere = "MyObject="1"" and DATABASE_TABLE_TOQUERY = "FavoriteObjects". These are all okay when viewed in an SQLite database viewer and it also returns a correct result if I execute the query using my SQLite database viewer.
public long findObject(String[] keys, String[] values,String DATABASE_TABLE_TOQUERY){
try {
if(keys.length<0 || keys.length!=values.length)
System.exit(-1);
String queryWhere = keys[0]+"=\""+values[0]+"\"";
for(int i=1;i<keys.length;i++) {
queryWhere+=" AND "+keys[i]+"=\""+values[i]+"\"";
}
Cursor mCursor = mDb.query(true, DATABASE_TABLE_TOQUERY, null,
queryWhere, null, null, null, null, null);
if (mCursor != null && mCursor.getCount()>0)
return Long.parseLong(mCursor.getString(mCursor.getColumnIndex(KEY_ROWID)));
}catch (SQLException e) {
return -1;
}
return -1;
}
When I get the results after executing the query statement, Cursor mCursor has at least one row, but when I access that row using mCursor.getString(X), it throws a weird error:
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
If I check mCursor.getColumnIndex(KEY_ROWID), I get a 0 value (which is correct). Anyone have any suggestions?
Thanks!
Jon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
Try