phongyewtong
phongyewtong

Reputation: 5319

How do I use sqflite and only query 1 row of data on flutter

How do I use sqflite and only query 1 row of data on flutter.

  Future<dynamic> queryOneDaily (int id, String date) async {
    final db = await database;
    return await db.query(
      Constants.dbTable, 
      where: "${Constants.dbColId} = ? AND ${Constants.dbColDate} = ?",
      whereArgs: [id, date],
    );
  }

Upvotes: 5

Views: 9041

Answers (1)

chunhunghan
chunhunghan

Reputation: 54427

From source code of sqlite
https://github.com/tekartik/sqflite/blob/master/sqflite/lib/sqlite_api.dart

You can use limit

code snippet

Future<dynamic> queryOneDaily (int id, String date) async {
    final db = await database;
    return await db.query(
      Constants.dbTable, 
      where: "${Constants.dbColId} = ? AND ${Constants.dbColDate} = ?",
      whereArgs: [id, date],
      limit: 1
    );
  }

code snippet of source code

/// @param limit Limits the number of rows returned by the query,
Future<List<Map<String, dynamic>>> query(String table,
      {bool distinct,
      List<String> columns,
      String where,
      List<dynamic> whereArgs,
      String groupBy,
      String having,
      String orderBy,
      int limit,
      int offset});

Upvotes: 7

Related Questions