与附近的语法错误QUOT; @域名" SQLite的
问题描述:
我使用的SQLite有一个问题。当我试图生我的查询这里是我遇到的错误。
I'm having an issue using SQLite. When i'm trying to raw my query here is the error I'm having.
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
下面是函数的原始查询:
Here is the function that raw the query :
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;
}
所有者
是字符串
包含这样的事情=>mail@domail.fr
owner
is a String
that contains something like this => "mail@domail.fr"
答
SQL字符串需要在单引号。
SQL string literals need to be in ''
single quotes.
不过,最好使用
占位符和绑定args作为文字:
However, it's better to use ?
placeholders and bind args for literals:
String selectQuery = "SELECT * FROM " + TABLE_SHOPPING_LIST + " WHERE " + SHOPPING_LIST_OWNER + " = ?";
Cursor cursor = db.rawQuery(selectQuery, new String[] { owner });