Get last inserted value from sqlite database Android

前端 未结 9 634
不思量自难忘°
不思量自难忘° 2020-11-29 07:11

I am trying to get the last inserted rowid from a sqlite database in Android. I have read a lot of posts about it, but can\'t get one to work. This is my method:



        
9条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 08:13

    /**
     * @return
     */
    public long getLastInsertId() {
        long index = 0;
        SQLiteDatabase sdb = getReadableDatabase();
        Cursor cursor = sdb.query(
                "sqlite_sequence",
                new String[]{"seq"},
                "name = ?",
                new String[]{TABLENAME},
                null,
                null,
                null,
                null
        );
        if (cursor.moveToFirst()) {
            index = cursor.getLong(cursor.getColumnIndex("seq"));
        }
        cursor.close();
        return index;
    }
    

提交回复
热议问题