How to query the sqflite database using the LIKE statement and the % or * sql wildcard
Asked Answered
D

7

5

I'm trying to query a Companies table using the companyName, I want to get the companies whose companyName(s) contains a query string, not only the companies whose companyName are exact matches of my query strings. If I was using sql, I would use the ---WHERE companyName LIKE %queryString% statement. So my question is how do I make such a query in sqflite? Right now I am getting a syntax error because of the %

Table Structure

CREATE TABLE Companies(
        id INTEGER PRIMARY KEY, 
        name TEXT,
        email TEXT, 
        website TEXT, 
        about TEXT, 
        phone TEXT,
        logo TEXT,
        created_at TEXT
      );
 );

Code for searching

Future<List<CompanyModel>> filterCompanies(String filterCriteria) async {
final db =await database;
List<CompanyModel> filteredCompanies = [];

var res = await db.rawQuery("SELECT * FROM Companies WHERE name LIKE %?%;", [filterCriteria]);
if(res.length !=null){
  for (var item in res){
    filteredCompanies.add(JobModel.fromDb(item));
  }

 } 
 return filteredCompanies;
}

When I add the %, I get a syntax error.

Devora answered 29/4, 2019 at 14:52 Comment(2)
Where is your code ? What is your db structure ? What have you found in the existing documentation that is relevant to your issue ?Leake
The existing documentation says nothing on the subject. I found nothing on the issue when I searched for ways of doing it. I edited the post with my codeDevora
R
22

The accepted answer leads to build error for me. I'm using sqflite: ^1.1.7+1 with flutter: v1.13.5.
From the discussion below, it comes out the percent character should be in the parameter instead of in the query, i.e.,

 res = await db.query(
  "Companies",
  where: "name LIKE ?"
  whereArgs: ['%$title']
 );

Ref: https://github.com/tekartik/sqflite/issues/316

Reeva answered 21/1, 2020 at 17:32 Comment(0)
O
12

I tried all of the answers above but not worked. Finally:

await db.query(
  "Companies",
  where:  "name LIKE ?",
  whereArgs: ['%$query%']
);
Otolaryngology answered 1/1, 2021 at 6:6 Comment(1)
This one is a working solution for me, thanks!Overstreet
A
6

The below code works:

var res = await db.rawQuery("SELECT * FROM Companies WHERE name LIKE '%$filterCriteria%'");

Achaea answered 4/6, 2019 at 21:28 Comment(1)
I guess the answer is , you need to put % on both sides of the LIKE variableExternal
D
3

This is the code that ended up working perfectly for me.

res = await db.query(
  "Companies",
  where: "name LIKE '%$?%'"
  whereArgs: [title]
);
Devora answered 25/6, 2019 at 12:27 Comment(0)
K
0

You have to use this syntax : LIKE " searched string "

Here is an example :

var res = await db.rawQuery(' SELECT * FROM Companies WHERE name LIKE "%$filterCriteria%" ');

It works with flutter and sqflite

Kleenex answered 12/2, 2020 at 21:28 Comment(0)
H
0

Use this

var res = await db.rawQuery("SELECT * FROM Companies WHERE name LIKE '%?%';", [filterCriteria]);

Instead of

var res = await db.rawQuery("SELECT * FROM Companies WHERE name LIKE %?%;", [filterCriteria]);

put like condition in single quote('...')

Honaker answered 13/2, 2020 at 9:54 Comment(0)
H
0

this works for me

var res = await db.rawQuery("SELECT * FROM Companies WHERE name LIKE ?",['%$filterCriteria%']);

Highhanded answered 20/4, 2020 at 22:14 Comment(1)
Hello! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Ofelia

© 2022 - 2025 — McMap. All rights reserved.