SQLite - 将ANDKE子句与AND子句一起使用

时间:2017-07-13 06:10:35

标签: android sqlite android-sqlite

所以我需要在我的数据库中搜索一个TABLE。

我创建了一个像这样的SQLite查询

String selection = COLUMN_1 + " LIKE ? AND " + COLUMN_2 + " ?= AND " + COLUMN_3 + " ?=";
String selectionArgs[] = {"%" + value1 + "%", value2 , value3 };

Cursor cursor = db.query(TABLE_1, null, selection, selectionArgs, null, null, null);

我收到以下错误

SQLiteException: near "?": syntax error (Sqlite code 1): , while compiling: SELECT * FROM TABLE_1 WHERE COLUMN_1 LIKE ? AND COLUMN_2 ?= AND COLUMN_3 ?=, (OS error - 2:No such file or directory)

在尝试了许多不同的变化和研究后,我放弃了并编写了一个rawQuery

String query = "SELECT * FROM " + ITEMS_CONTENT_TABLE + " WHERE " + ITEM_NAME + " LIKE '%" + name + "%' AND " + BASE_CATEGORY_NAME + " = '" + baseCategoryName + "' AND " + BASE_CATEGORY_SRC_PATH + " = '" + baseCategorySrcPath + "';";

Cursor cursor = db.rawQuery(query , null);

无缝地工作!

我的问题是为什么前者不起作用?如果它确实是语法错误,那么它也不应该在后面的例子中工作。

2 个答案:

答案 0 :(得分:2)

更改查询如下

String selection = COLUMN_1 + " LIKE ? AND " + COLUMN_2 + " =? AND " + COLUMN_3 + " =?"

答案 1 :(得分:0)

以下查询在我的情况下正常工作..

 Cursor c = db.rawQuery("Select ConsAcNo From FieldUtilityData Where ConsAcNo like '%" + txtConsAcNo.getText() + "%' and ProjectNo = '" + gp.city + "' and IsCompleted = 'N' and ConsAcNo not in (SELECT CONS_ACCOUNT_NO FROM FieldTestDataFinal) Limit 15", null);