Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know if row is exist in flutter sqflite?

I have to know a specific row is exist where uidCol column is aaa in tagtable. But I didn't know so I'm just using try~catch block.

What I want to do is to check local DB and if there are no data, fetching from firestore.

What I'm doing is like below

try {
  await db.rawQuery('SELECT * FROM tagTable WHERE uidCol="aaa"');
} catch(exception){
  await _fetchTagsFromFirestore().catchError((e){
    print('FATAL ERROR: ${e.toString()}');
    return FETCH_RESULT.FAILURE;
  });
}

But I think it is not right way to check row is exist. How can I deal with properly?

like image 496
Privacy of Animal Avatar asked Nov 15 '25 09:11

Privacy of Animal


2 Answers

I'm assuming you want to check if there's a record that exists with the specified criteria in the database and do something if it does exist.

getting the result from the database and storing in a queryResult: var queryResult = await db.rawQuery('SELECT * FROM tagTable WHERE uidCol="aaa"');

checking if the result is empty:

 result.isNotEmpty ? //do something if the result is not empty here
 : []; //else return empty list
like image 183
Salma Avatar answered Nov 17 '25 09:11

Salma


Try this it worked for me

var db= DatabaseHelper();

Future<int> getcount(id) async {
      var dbclient = await db;
      int  count = Sqflite.firstIntValue(
          await dbclient.rawQuery("SELECT COUNT(*) FROM $cartTable WHERE $columnid=$id"));
      return count;
      }
like image 22
moshire Avatar answered Nov 17 '25 07:11

moshire