java - android 如何从数据库中删除第一个插入的项目

标签 java android sqlite android-sqlite

假设我要插入三个数据。 所以最初,我插入了预告片 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/

相关文章:

java - 我可以使用基本身份验证通过 GET REST 请求从 JAVA 访问 SharePoint 2010 吗?

java - 尝试将一个类中的信息刷新到java中的另一个类中

android - 如何在平板电脑上实现这种布局?

php - 使用php从android访问数据库获取 "<HTML>"而不是查询结果

java - SQLite android 不创建所有表

java - 如何禁用 p :commandButton? 的回车键

java - 静态同步方法和非静态同步方法都可以作用于静态成员变量,这可能会导致意外行为

angular - SQLite Mocking 不适用于 ionic

android - "Your content must have a ListView whose id attribute is ' android.R.id.list '"当从ListActivity变为 View 时

python - 操作错误: foreign key mismatch