简体   繁体   English

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

[英]SQLite - Using LIKE Clause with AND Clause

So I needed to search through a TABLE in my Database. 因此,我需要在数据库中搜索一个表。

I created a SQLite Query like so 我像这样创建了一个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);

I got the following error 我收到以下错误

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)

After trying many different variations and research I gave up and wrote a rawQuery 在尝试了许多不同的变体和研究之后,我放弃了并编写了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);

Which worked seamlessly! 哪个无缝工作!

My question is why is the former not working? 我的问题是为什么前者不起作用? If it really was syntax error then it shouldn't be working in the later example either. 如果确实是语法错误,则在以后的示例中也不应该起作用。

如下更改查询

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

下面的查询在我的情况下工作正常。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM