如何在 SQlite 中获取最后插入的行 android
How to get last inserted row in SQlite android
我想获取最后插入的行的自动设置的主键值(自动递增)。我搜索了 S/O 并找到了最后一个 row_id 的答案。但我需要最后插入的行的值。请帮助我。
谢谢
试试这个代码
String selectQuery = "SELECT * FROM " + "sqlite_sequence";
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToLast();
或
SELECT *
FROM TABLE
WHERE ID = (SELECT MAX(ID) FROM TABLE);
或
SELECT * FROM table ORDER BY column DESC LIMIT 1;
试试这个:
select * from <TABLE> where row_id = (select max(row_id) from <TABLE>);
或
SELECT * FROM tablename ORDER BY row_id DESC LIMIT 1;
使用moveToLast()
in Cursor
界面。
/**
* Move the cursor to the last row.
*
* <p>This method will return false if the cursor is empty.
*
* @return whether the move succeeded.
*/
boolean moveToLast();
简单示例:
final static String TABLE_NAME = "table_name";
String name;
//....
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
if(cursor.moveToLast()){
name = cursor.getString(column_index);
//--get other cols values
}
如果想在插入时获取数据,可以使用insert()
in SQLiteDatabase
class。
private static final String TABLE_NAME = "table_name";
private static final String COLUMN_ONE = "ID";
private static final String COLUMN_TWO = "NAME";
long row = 0;//to get last row
//.....
SQLiteDatabase db= this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_TWO, name);//name is a variable for data value
row = db.insert(TABLE_NAME, null, contentValues);
//insert() returns the row ID of the newly inserted row, or -1 if an error occurred
Cursor res = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE ID = ?", new String[] { row+"" });
while (res.moveToNext()) {//there is no need to use while, if condition is good.
System.out.println(res.getString(0));//I just sout to avoid more code.
System.out.println(res.getString(1));
}
我想获取最后插入的行的自动设置的主键值(自动递增)。我搜索了 S/O 并找到了最后一个 row_id 的答案。但我需要最后插入的行的值。请帮助我。
谢谢
试试这个代码
String selectQuery = "SELECT * FROM " + "sqlite_sequence";
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToLast();
或
SELECT *
FROM TABLE
WHERE ID = (SELECT MAX(ID) FROM TABLE);
或
SELECT * FROM table ORDER BY column DESC LIMIT 1;
试试这个:
select * from <TABLE> where row_id = (select max(row_id) from <TABLE>);
或
SELECT * FROM tablename ORDER BY row_id DESC LIMIT 1;
使用moveToLast()
in Cursor
界面。
/**
* Move the cursor to the last row.
*
* <p>This method will return false if the cursor is empty.
*
* @return whether the move succeeded.
*/
boolean moveToLast();
简单示例:
final static String TABLE_NAME = "table_name";
String name;
//....
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
if(cursor.moveToLast()){
name = cursor.getString(column_index);
//--get other cols values
}
如果想在插入时获取数据,可以使用insert()
in SQLiteDatabase
class。
private static final String TABLE_NAME = "table_name";
private static final String COLUMN_ONE = "ID";
private static final String COLUMN_TWO = "NAME";
long row = 0;//to get last row
//.....
SQLiteDatabase db= this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_TWO, name);//name is a variable for data value
row = db.insert(TABLE_NAME, null, contentValues);
//insert() returns the row ID of the newly inserted row, or -1 if an error occurred
Cursor res = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE ID = ?", new String[] { row+"" });
while (res.moveToNext()) {//there is no need to use while, if condition is good.
System.out.println(res.getString(0));//I just sout to avoid more code.
System.out.println(res.getString(1));
}