How to use wildcards in a SQLite query?

Muhammad Faizan

I am trying to fetch data which contains specific string but query is not working, following is method for fetching data.

public Cursor getSearch(String item) {
    SQLiteDatabase db = this.getReadableDatabase();
    String mQuery = "SELECT * FROM hadiths WHERE text LIKE %"+item.toString()+"%";
    Cursor cursor = db.rawQuery(mQuery, null);
    return cursor;
}

Logcat shows following error.

Caused by: android.database.sqlite.SQLiteException: near "%": syntax error (code 1): , while compiling: SELECT * FROM hadiths WHERE text LIKE %fast%

I know that the wildcard %% and string variable item is causing issue but how do I use string variable along with wildcard?

Barns

You need to add single quotes to the query.

String mQuery = "SELECT * FROM hadiths WHERE text LIKE '%"+item+"%'";

Take a look at the SQLite docs: https://www.tutorialspoint.com/sqlite/sqlite_like_clause.htm

注:toString()「item」はすでにタイプであるため、使用する必要はありませんString

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to use a query in another query?

分類Dev

PHP mysql query using wildcards

分類Dev

How to use the WITH clause in an SQLite trigger

分類Dev

How to display my sqlite query with separator

分類Dev

How to write a sqlite query to get specific data?

分類Dev

Which method to use to search huge records QueryAsync or Query in SQLite

分類Dev

How to use the results of one query in the next query?

分類Dev

VLOOKUP style query with wildcards in range portion

分類Dev

How to use sqlite3_config(SQLITE_CONFIG_HEAP...)?

分類Dev

how to use update query in mongoDb?

分類Dev

how to use update query in mongoDb?

分類Dev

How to use percentage in a Laravel query

分類Dev

How to use a default in SELECT query?

分類Dev

How to use NOT in relational SQL query

分類Dev

how to use substring function sqlite android

分類Dev

How to use Dapper.QueryMultiple with Sqlite

分類Dev

Can I use wildcards to compare strings?

分類Dev

Specific use case for wildcards in Notepad++

分類Dev

Android: how to do an inner-join query in sqlite using the ContextResolver?

分類Dev

How to use variable in sql query odoo

分類Dev

How to use variables inside query in Pandas?

分類Dev

How to get a mysql query to use a specific index?

分類Dev

how to use mongoose pagination with populate and query

分類Dev

How to use SQL Hints on Java query

分類Dev

How to use nested mongoDB query to calculate percentage?

分類Dev

How to use "map" data structure "in a query"

分類Dev

How to use Query Builder when different conditions

分類Dev

How to use common parameter for Mysql PDO query

分類Dev

How to use LINQ to query a subset of string

Related 関連記事

ホットタグ

アーカイブ