tekartik / sqflite

SQLite flutter plugin
BSD 2-Clause "Simplified" License
2.86k stars 524 forks source link

can we use database.transaction((txn) for select query #938

Open rammesaran opened 1 year ago

rammesaran commented 1 year ago

Hi The below query is not getting called. My question is can we use db.transaction((txn) for select query?

  Future getTotalCountInWalletRecent() async {
    try {
      final db = await initializeDB();
      await db.transaction((txn) async {
        List<Map> results = await txn
            .rawQuery("SELECT count(*) as totalCount from Walletrecent");
        return results;
      });
    } catch (e, s) {
      FirebaseCrashLogs.logFatelError(
          error: e,
          stackTrace: s,
          reason:
              "Exception in getTotalCountInWalletRecent Method in DbFunctions.dart");
    }
  }

By using Below Query iam able to get the response - But i am facing database lock issue:


  Future getTotalCountInWalletRecent() async {
    try {
      final db = await initializeDB();

        List<Map> results = await db
            .rawQuery("SELECT count(*) as totalCount from Walletrecent");
        return results;

    } catch (e, s) {
      FirebaseCrashLogs.logFatelError(
          error: e,
          stackTrace: s,
          reason:
              "Exception in getTotalCountInWalletRecent Method in DbFunctions.dart");
    }
  }

Kindly let me know what i am doing wrong here!!! Any help would be appreciated.

Thanks

alextekartik commented 1 year ago

In the first example, you might want to return the result from the transaction (the inner list returned):

return await db.transaction((txn) async {
   ...

instead of:

await db.transaction((txn) async {
   ...