我在使用SQLite时遇到了问题.当我试图生成我的查询时,这是我遇到的错误.
03-05 11:40:54.916: E/AndroidRuntime(6136): Caused by: android.database.sqlite.SQLiteException: near "@domain": syntax error (code 1): , while compiling: SELECT * FROM shopping_list WHERE shopping_list_owner = mail@domain.fr
Run Code Online (Sandbox Code Playgroud)
这是查询原始的函数:
public List<ShoppingListModel> getAllShoppingListByOwner(String owner) {
List<ShoppingListModel> shoppingList = new ArrayList<ShoppingListModel>();
String selectQuery = "SELECT * FROM " + TABLE_SHOPPING_LIST + " WHERE " + SHOPPING_LIST_OWNER + " = " + owner;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
ShoppingListModel list = new ShoppingListModel(cursor.getString(cursor.getColumnIndex(SHOPPING_LIST_NAME)),
cursor.getInt(cursor.getColumnIndex(SHOPPING_LIST_ID)),
cursor.getString(cursor.getColumnIndex(SHOPPING_LIST_DATE_CREATION)),
cursor.getString(cursor.getColumnIndex(SHOPPING_LIST_OWNER)));
shoppingList.add(list);
} while (cursor.moveToNext());
}
return shoppingList;
}
Run Code Online (Sandbox Code Playgroud)
owner是String包含这样的东西=>"mail@domail.fr"
SQL字符串文字需要使用''单引号.
但是,最好使用?占位符并为文字绑定args:
String selectQuery = "SELECT * FROM " + TABLE_SHOPPING_LIST + " WHERE " + SHOPPING_LIST_OWNER + " = ?";
Cursor cursor = db.rawQuery(selectQuery, new String[] { owner });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
436 次 |
| 最近记录: |