Android SQL 删除行不起作用

发布于 2024-11-28 04:40:28 字数 674 浏览 0 评论 0原文

首先,我在助手类中创建一个数据库,如下所示:

public void onCreate(SQLiteDatabase db) {
    String sql = "create table " + TABLE + "( " + ID
            + " integer primary key autoincrement, " + FIELD1 + " text, "
            + FIELD2 + " text);";
    Log.d("EventsData", "onCreate: " + sql);
    db.execSQL(sql);
}// Where public static final String ID = "_id"; ect.

然后我插入了数据,并且我看到它通过在 TextView 中显示插入的数据来工作。当我需要删除一行时,我的问题就出现了。

我可以使用

db.delete(TABLE, null , null); 删除所有内容(我再次看到这有效)

但是,如果我将其更改为删除单行,例如

db. delete(TABLE, "_id" + Index, null); 其中,例如 int Index =4;

什么也没发生,我没有收到任何错误,也没有删除。

任何人都可以帮忙解释为什么会发生这种情况吗?

Firstly I am creating a Database in a helper class as follows:

public void onCreate(SQLiteDatabase db) {
    String sql = "create table " + TABLE + "( " + ID
            + " integer primary key autoincrement, " + FIELD1 + " text, "
            + FIELD2 + " text);";
    Log.d("EventsData", "onCreate: " + sql);
    db.execSQL(sql);
}// Where public static final String ID = "_id"; ect.

I have then inserted data and I have seen it works by displaying the inserted data in a TextView. My problems come when I need to delete a row.

I can deleted everything by using

db.delete(TABLE, null , null); (again I can see that this works)

However if I change it to deleted a single row such as

db.delete(TABLE, "_id" + Index, null); Where for example int Index =4;

nothing happens, I get no errors and no delete.

Can anyone help with why this happens?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抽个烟儿 2024-12-05 04:40:28
SQLiteDatabase db = this.getWritableDatabase();    
db.delete(TABLE_CONTACT, _ID + "=" + _id, null);
db.close();
SQLiteDatabase db = this.getWritableDatabase();    
db.delete(TABLE_CONTACT, _ID + "=" + _id, null);
db.close();
泅渡 2024-12-05 04:40:28

尝试

db.delete(TABLE, "_id = ?", new String[] { "" + Index });

Try

db.delete(TABLE, "_id = ?", new String[] { "" + Index });
熟人话多 2024-12-05 04:40:28

db.delete(TABLE, "_id=" + Index, null);

db.delete(TABLE, "_id=" + Index, null);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文