假设我要插入三个数据。 所以最初,我插入了预告片 1、预告片 2、预告片 3。 这里的id是自增的。
id trailer_number trailder_id
1 trailer1 766
2 trailer2 899
3 trailer3 577
在此如何从表中删除第一个插入的记录。 我需要从按钮中删除,首先删除预告片 1,然后删除预告片 2,然后删除预告片 3。
我尝试使用这个,但总是得到 0。
public static final String TABLE_NAME = "trailer_table";
public int getFirstId() {
int _id = 0;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[] {"trailer_number"}, null, null, null, null, null);
if (cursor.moveToLast()) {
_id = cursor.getInt(0);
}
cursor.close();
db.close();
return _id;
}
最佳答案
SQLiteDatabase mDb = dbHelper.getWritableDatabase();
String sql1 = "DELETE FROM "+TABLE_NAME+" WHERE id =min(id);";
try {
mDb.execSQL(sql1);
} catch (Exception e) {
} finally {
mDb.close();
}
每次运行此代码都会删除插入的第一个项目
关于java - android 如何从数据库中删除第一个插入的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58077030/