且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Android的SQLite数据库中删除

更新时间:2023-01-03 11:04:21

在数据库中的第一个记录不会总是具有0的ID。每次插入新记录的主键将自动加1递增所以,你需要的是得到的第一个记录的ID,并执行使用该ID的删除。

The first record in the database will not always have an ID of "0". Each time you insert a new record the primary key will increment by 1. So what you need is to get the id of the first record, and perform the delete using that id.

Cursor cursor = db.query(DATABASE_TABLE, null, null, null, null, null, null); 

    if(cursor.moveToFirst()) {
        long rowId = cursor.getLong(cursor.getColumnIndex(KEY_ROWID)); 

        db.delete(DATABASE_TABLE, KEY_ROWID +  "=" + rowId, null);
   }