count method

Future<int> count(
  1. {@required String table,
  2. String where,
  3. String columns = "id",
  4. bool verbose = false}
)

count rows in a table

Implementation

Future<int> count(
    {@required String table,
    String where,
    String columns = "id",
    bool verbose = false}) async {
  /// [table] is the table to use and [where] the sql where clause
  ///
  /// Returns a future with the count of the rows
  try {
    if (!_isReady) {
      throw DatabaseNotReady();
    }
    final timer = Stopwatch()..start();
    var w = "";
    if (where != null) {
      w = " WHERE $where";
    }
    final q = 'SELECT COUNT($columns) FROM $table$w';
    int c;
    await _db.transaction((txn) async {
      c = Sqflite.firstIntValue(await txn.rawQuery(q));
    });
    timer.stop();
    if (verbose) {
      final msg = "$q in ${timer.elapsedMilliseconds} ms";
      print(msg);
    }
    return c;
  } catch (e) {
    rethrow;
  }
}