Android sqlite删除行失败

发布于 2024-12-03 17:27:56 字数 599 浏览 0 评论 0原文

我正在尝试删除表中我知道该行存在的行。我尝试过:

final String s = "DELETE from %s where Name = '%s'";        
String sql = String.format(s, GetTableName(), sListName);
Cursor cr= GetUserDb().GetSQLiteDb().rawQuery(sql, null);

并且我尝试过:

String sWhere = String.format("Name = '%s'", sListName);
long lRowsUpdated = GetUserDb().GetSQLiteDb().delete(GetTableName(), sWhere, null);


** sWhere >> Name = 'groceries' **
** sql >> DELETE from Lists where Name = 'groceries' **

没有崩溃或logcat异常,但该行仍然没有被删除。我有什么遗漏的吗?也许我在其他地方锁定了它,或者我需要在清单中设置某些权限或其他内容?

I'm trying to delete a row in a table where I know the row exists. I tried:

final String s = "DELETE from %s where Name = '%s'";        
String sql = String.format(s, GetTableName(), sListName);
Cursor cr= GetUserDb().GetSQLiteDb().rawQuery(sql, null);

and I tried:

String sWhere = String.format("Name = '%s'", sListName);
long lRowsUpdated = GetUserDb().GetSQLiteDb().delete(GetTableName(), sWhere, null);


** sWhere >> Name = 'groceries' **
** sql >> DELETE from Lists where Name = 'groceries' **

There is no crash or logcat exception but the row is still not deleted. Is there something I'm missing? Maybe I have a lock on it somewhere else or I need to set certain permissions in my Manifest or something?

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

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

发布评论

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

评论(3

摘星┃星的人 2024-12-10 17:27:57

试试这个:

       SQLiteStatement stmt = db.compileStatement("DELETE FROM " + getTableName() + "WHERE Name=?");
       stmt.bindString(1, sListName);
   stmt.execute();

Try this:

       SQLiteStatement stmt = db.compileStatement("DELETE FROM " + getTableName() + "WHERE Name=?");
       stmt.bindString(1, sListName);
   stmt.execute();
待天淡蓝洁白时 2024-12-10 17:27:57

如果您尝试使用键的列是 sListName,则您应该删除其中 sListName = '%s',除非您尝试删除另一列名为 Name 的列,但如果这不是您的主键您最终可能会在删除中得到两行。

您始终可以使用模拟器和 adb shell,只需运行 sqlite3 shell 命令并尝试 SQL 语句。如果你输入你的并得到 0 行受影响,你就知道你的语句被搞乱了,而不是你的 java.sql.

如果您没有使用任何内置的 CurorAdapters 或 ContentProviders,您的主键不需要命名为 _ID,它可以命名为任何名称。至于类型,在 SQLite3 中,它实际上只是关于如何转换它的建议,您可以将任何数据放入您想要的任何列中。

if the column you're trying to have your key by is sListName you should have delete where sListName = '%s' unless you have another column that is called Name that you are trying to delete, but if that isn't your primary key you might end up getting two rows in your delete.

You can always use the emulator and an adb shell and just go run the sqlite3 shell command and try your SQL statements. If you type yours in and get 0 rows affected, you know you're statement is messed up, not your java.

If you're not using any built in CurorAdapters or ContentProviders your primary key does not need to be named _ID, it can be named whatever. As for the type, in SQLite3, it's really just a suggestion on how to cast it, you can put whatever data in whatever column you want.

夜还是长夜 2024-12-10 17:27:56

使用 SqliteDatabase 中的 delete() - 这将返回受影响的行数。例如delete(tablename, "name=?", new String[] { aString } )

Use delete() from SqliteDatabase - this returns the count of affected rows. E.g. delete(tablename, "name=?", new String[] { aString } )

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