以下sqlite查询不会删除以零开头的ID。
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);
我将BUS_NUM保留为文本,而不是出于某种目的而保留为int。
这是我调用删除行的函数..
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
}
当代码不以零开头时,此代码非常正常。
适用于76555,而不适用于09877.我的代码错误
答案 0 :(得分:29)
此代码生成的查询结束如下:
DELETE FROM BusTable WHERE BusNum = 012345
数据库会将其解释为数字,而不是字符串。
在SQL中,必须引用字符串:
DELETE FROM BusTable WHERE BusNum = '012345'
但是,您可以避免使用参数格式化字符串:
db.delete(TABLE_BUS, KEY_BUS_NUM + " = ?", new String[] { bus_num });
答案 1 :(得分:9)
也许..
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();
}
答案 2 :(得分:0)
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())});
}