Android sqlite删除行失败
我正在尝试删除表中我知道该行存在的行。我尝试过:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
Try this:
如果您尝试使用键的列是 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.
使用 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 } )