[英]How to use sqlite to retrieve single result?
I have this code that retrieves the last id 我有这段代码可以检索最后一个ID
public int retrieveLastId() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select "+ RECORDING_ID +" from " + DATABASE_TABLE + " ORDER BY "+ RECORDING_ID +" DESC LIMIT 1 ", null);
int last = 0;
last = Integer.parseInt(cursor.getString(0));
Log.e("sfsdlkfjs dfl sd",cursor.getString(0));
return last;
}
but I get an error android.database.cursorindexoutofboundsexception how do I return the last id properly? 但我收到错误android.database.cursorindexoutofboundsexception如何正确返回最后一个ID?
This cause of error is you do not check cursor is null . 导致此错误的原因是您不检查游标是否为null。
if (cursor.moveToFirst()) // Check your cursor has value?
System.out.println(cursor.getString(0);
cursor.close(); //you should realse cursor
Make sure you have values in the table. 确保表中有值。 If it doesn't have it will end up with exception with your code.
如果没有它,最终将导致代码异常。
Try doing this changes in your code. 尝试在您的代码中进行此更改。
Cursor cursor = database.rawQuery(selectQuery, null);if(cursor!=null && cursor.getCount()>0){cursor.moveToFirst()
do{
bankbalresult = cursor.getString(0);
} while (cursor.moveToNext());}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.