android sqlite删除查询不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25328565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android sqlite delete query not working
提问by Indra Kumar S
The following sqlite query does not delete IDs which starts with zero.
以下 sqlite 查询不会删除以零开头的 ID。
Android Sqlite Table structure
Android Sqlite 表结构
String CREATE_TABLE_BUS = "CREATE TABLE " + TABLE_BUS + "("
+ KEY_TID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_BUS_NUM + " TEXT,"
+ KEY_BUS_NAME + " TEXT,"
+ KEY_FROM + " TEXT,"
+ KEY_TO + " TEXT,"
+ KEY_TYPE + " TEXT"
+ ")";
db.execSQL(CREATE_TABLE_BUS);
I kept BUS_NUM as text and not as int for some purpose.
出于某种目的,我将 BUS_NUM 保留为文本而不是 int。
And this is the function I call to delete a row..
这是我调用删除行的函数。
public void Delete_Bus(String bus_num) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_BUS, KEY_BUS_NUM+"="+bus_num , null);
Log.e("deleting bus num", bus_num);
db.close(); // Closing database connection
}
This code works very fine when the code does not start with zero..
当代码不以零开头时,此代码工作得很好..
It works for 76555 and not for 09877. Whats wrong with my code
它适用于 76555 而不适用于 09877。我的代码有什么问题
回答by CL.
The query generated by this code ends like this:
此代码生成的查询以这样的方式结束:
DELETE FROM BusTable WHERE BusNum = 012345
The database will interpret this as a number, not a string.
数据库会将其解释为数字,而不是字符串。
In SQL, strings must be quoted:
在 SQL 中,字符串必须被引用:
DELETE FROM BusTable WHERE BusNum = '012345'
However, you can avoid having to format the string by using a parameter:
但是,您可以避免使用参数来格式化字符串:
db.delete(TABLE_BUS, KEY_BUS_NUM + " = ?", new String[] { bus_num });
回答by Ozan
Maybe..
也许..
public void Delete_Bus(String bus_num)
{
SQLiteDatabase db=this.getWritableDatabase();
db.execSQL("DELETE FROM "+TABLE_BUS+" WHERE "+KEY_BUS_NUM+"='"+bus_num+"'");
db.close();
}
回答by Ali Eslami
public void Remove_Bookmarked(Bookmark bookmark) {
SQLiteDatabase db = this.getReadableDatabase();
db.delete(Bookmark_TABLE_NAME, COL_ID_CONTENT + "= ? and " + COL_TYPE + " = ?", new String[]{String.valueOf(bookmark.getId_content()), String.valueOf(bookmark.getType())});
}