How to get Last record from Sqlite?

前端 未结 12 2101
悲&欢浪女
悲&欢浪女 2020-12-02 07:37

I have a one table question_table and one ImageButton (Back). I need to get the last inserted record from the database after clicking on t

12条回答
  •  误落风尘
    2020-12-02 08:31

    Suppose you are looking for last row of table dbstr.TABNAME, into an INTEGER column named "_ID" (for example BaseColumns._ID), but could be anyother column you want.

    public int getLastId() {
        int _id = 0;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(dbstr.TABNAME, new String[] {BaseColumns._ID}, null, null, null, null, null);
    
        if (cursor.moveToLast()) {
            _id = cursor.getInt(0);
        }
    
        cursor.close();
        db.close();
        return _id;
    }
    

提交回复
热议问题