“@domain”SQLite附近的语法错误

时间:2014-03-05 10:51:29

标签: android sqlite

我在使用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,其中包含类似这样的内容=&gt; “mail@domail.fr”

4 个答案:

答案 0 :(得分:6)

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

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

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

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

答案 1 :(得分:2)

尝试这样

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

答案 2 :(得分:1)

所有者应该用引号括起来。

String selectQuery = "SELECT * FROM " + TABLE_SHOPPING_LIST + " WHERE " + SHOPPING_LIST_OWNER + "=\'" + owner + '\'';

答案 3 :(得分:1)

由于您的变量所有者是String类型,因此您需要添加开始和结束'单引号。请按照以下方式使用您的查询,

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