假设您正在查找表tb_itembank的最后一行到名为“_id”的列(例如BaseColumns._ID),但可能是您想要的任何其他列。
public int getLastId() {
int _id = 0;
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(new String[] { KEY_ItemBank_ID,
KEY_ItemBank_No,
KEY_ItemBank_Name,
KEY_ItemBank_Stems,
KEY_ItemBank_Choices,
KEY_ItemBank_Answer,
KEY_ItemBank_CREATE,
KEY_ItemBank_MODIFIED
}, null, null, null,null,"_id DESC", "1");使用分页查询查询最后一条记录
if (cursor.moveToLast()) {
_id = cursor.getInt(0);
}
cursor.close();
db.close();
return _id;
}
//题库表
private static final String DB_TABLE_ItemBank="tb_itembank";
private static final String KEY_ItemBank_ID="_id";
private static final String KEY_ItemBank_No="itembank_no";
private static final String KEY_ItemBank_Name="itembank_name";
private static final String KEY_ItemBank_Stems="itembank_stems";
private static final String KEY_ItemBank_Choices="itembank_choices";
private static final String KEY_ItemBank_Answer="itembank_answer";
private static final String KEY_ItemBank_CREATE="itembank_create";
private static final String KEY_ItemBank_MODIFIED="itembank_modified";
//建立题库表的语句
private static final String DB_CREATE_TB_ItemBank="create table IF NOT EXISTS "+DB_TABLE_ItemBank+
"("+KEY_ItemBank_ID+" integer primary key autoincrement," +
KEY_ItemBank_No+" integer,"+
KEY_ItemBank_Name+" text,"+
KEY_ItemBank_Stems+" text,"+
KEY_ItemBank_Choices+" text,"+
KEY_ItemBank_Answer+" text,"+
KEY_ItemBank_CREATE+" date,"+
KEY_ItemBank_MODIFIED+" date);";
public void onCreate(SQLiteDatabase _db){
_db.execSQL(DB_CREATE_TB_ItemBank); }
public void onUpgrade(SQLiteDatabase _db,int _oldVersion,int _newVersion){
_db.execSQL("DROP TABLE IF EXISTS "+DB_TABLE_ItemBank);}
query( table, columns, selection, selectionArgs, groupBy, having, orderBy, limit );
参数含义:
table:表名。
columns:要查询出来的列名。
selection:查询条件子句。
selectionArgs:对应于selection语句中占位符的值。
groupBy:分组。相当于select语句group by关键字后面的部分。
having:分组后聚合的过滤条件。相当于select语句having关键字后面的部分。
orderBy:排序。相当于select语句order by关键字后面的部分 ASC或DESC。
limit:指定偏移量和获取的记录数。
或者是
SELECT *
FROM TABLE
WHERE ID = (SELECT MAX(ID) FROM TABLE);