Help please! Error when accessing Data within a cursor returned by SQLite for android
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 != n开发者_开发百科ull && 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
Try
mCursor.moveToNext();
精彩评论