Использование moveToLast()
в Cursor
интерфейсе.
От android.googlesource.com
/**
* 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()
в 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));
}
This is post is about autoincrement, if you want to read.
показать, что создать таблицу запроса? –
Что вы делали до сих пор? Некоторая часть кода была бы намного полезнее .. –
Возможный дубликат [Получить сгенерированный идентификатор после вставки] (http://stackoverflow.com/questions/5409751/get-generated-id-after-insert) –