gpt4 book ai didi

android - "@domain"SQLite 附近的语法错误

转载 作者:太空狗 更新时间:2023-10-29 15:41:44 26 4
gpt4 key购买 nike

我在使用 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

这是原始查询的函数:

    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;
}

owner 是一个 String 包含类似这样的内容 => "mail@domail.fr"

最佳答案

SQL 字符串文字需要放在 '' 单引号中。

但是,最好使用 ? 占位符并为文字绑定(bind) args:

String selectQuery = "SELECT * FROM " + TABLE_SHOPPING_LIST + " WHERE " + SHOPPING_LIST_OWNER + " = ?";

Cursor cursor = db.rawQuery(selectQuery, new String[] { owner });

关于android - "@domain"SQLite 附近的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22195612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com