简体   繁体   English

如何使用sqlite检索单个结果?

[英]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.

相关问题 如何从 SQLite 数据库中检索单个图像 - How to retrieve a single image from a SQLite database 如何从Google检索单个结果? - How to retrieve Single result from google? 当要更新时,如何通过ContentValues使用sqlite更新取决于当前值?如何检索结果? - How can use sqlite update via ContentValues when want to update depend on current value and how can retrieve the Result? 如何在Sqlite java android中检索选择查询的结果? - How to retrieve the result of a select query in Sqlite java android? 如何检索单个sqlite列并将行存储到数组中 - How to retrieve single sqlite column and store rows into array 如何使用sqlite数据库存储获取数据和检索 - How to use sqlite database to store fetch data and retrieve 如何在sqlite中检索结果 - How to retrieve results in sqlite SQLite命令以单个号码检索多个消息 - Sqlite command to retrieve multiple message with single number 如何检索最后输入的结果并在存储到SQLite Android之后执行减法 - How to Retrieve Last Entered Result and Perform Subtraction after that store into SQLite Android 如何从特定行和列的 SQLite 数据库中检索单个数据? - How to retrieve a single data from SQLite Database from specific Row And Column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM