我正在根据输入数据进行过滤后尝试更新SQLite数据库。下面是我的代码
public void insert_checkbox(String Str1, String chkboxValue){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_CHECKBOX, Str1 );
db.execSQL("UPDATE " + ResturantEntry.TABLE_NAME + " SET COLUMN_CHECKBOX = '" + Str1 + "' WHERE COLUMN_LOCATION = '" + chkboxValue + "'");
this.getWritableDatabase().insertOrThrow(ResturantContract.ResturantEntry.TABLE_NAME,"",contentValues);
}
}
我在SQLiteOpenhelper中创建数据库代码如下
p
ublic void onCreate(SQLiteDatabase db) {
String SQL_CREATE_RESTURANTS_TABLE = "CREATE TABLE " + ResturantEntry.TABLE_NAME + "("`enter code here`
+ ResturantEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ ResturantEntry.COLUMN_NAME + " TEXT,"
+ ResturantEntry.COLUMN_TYPE + " TEXT,"
+ ResturantEntry.COLUMN_ADDRESS + " TEXT,"
+ ResturantEntry.COLUMN_LOCATION + " TEXT,"
+ ResturantEntry.COLUMN_GPSCOORDINATES + " INTEGER,"
+ ResturantEntry.COLUMN_TELEPHONE + " INTEGER,"
+ ResturantEntry.COLUMN_OPENINGHOURS + " INTEGER,"
+ ResturantEntry.COLUMN_CLOSINGHOURS + " INTEGER,"
+ ResturantEntry.COLUMN_CHECKBOX + " TEXT,"
+ ResturantEntry.COLUMN_CHECKBOXVALUE + " TEXT);";
db.execSQL(SQL_CREATE_RESTURANTS_TABLE);
}
我不断在下面的日志中收到错误
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such column: COLUMN_CHECKBOX (code 1): , while compiling: UPDATE Resturant SET COLUMN_CHECKBOX = '1' WHERE COLUMN_LOCATION = 'Ajah')
#################################################################
答案 0 :(得分:0)
COLUMN_CHECKBOX
是包含列名而不是文字列名的变量名。您不应在SQL中将其放在双引号中。与COLUMN_LOCATION
相同。
但是,与其在execSQL()
中更正SQL,不如在SQLiteDatabase
中使用update()
ContentValues
方法,会更好。它还可以为您处理诸如SQL注入保护之类的事情。
答案 1 :(得分:0)
WHERE
进行过滤(通过在ResturantEntry._ID
子句中使用),然后执行更新。您似乎没有那个。ContentValues
和直接UPDATE
语句。您不需要它们,都选择一个。答案 2 :(得分:0)
请尝试使用SQLiteDatabase更新方法更新代码:-
public int insert_checkbox(String Str1, String chkboxValue)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_CHECKBOX, Str1 );
String selection = ResturantEntry.COLUMN_LOCATION + " =? ";
String[] selectionArgs = new String[]{chkboxValue};
//count denotes the number of rows update
int count = db.update(ResturantEntry.TABLE_NAME, contentValues,selection ,selectionArgs );
retun count;
}
希望这会有所帮助。