import 'package:sqflite/sqflite.dart'; import 'package:path/path.dart'; class CheckinDBHelper { static final _databaseName = "EventDatabase.db"; static final _databaseVersion = 1; static final table = 'event_table'; static final columnLocationId = 'location_id'; static final columnOrderId = 'order_id'; static final columnCheckinTime = 'checkin_time'; static final columnEventName = 'event_name'; // make this a singleton class CheckinDBHelper._privateConstructor(); static final CheckinDBHelper instance = CheckinDBHelper._privateConstructor(); // only have a single app-wide reference to the database static Database? _database; Future get database async { if (_database != null) return _database!; _database = await _initDatabase(); return _database!; } // this opens the database (and creates it if it doesn't exist) _initDatabase() async { String path = join(await getDatabasesPath(), _databaseName); return await openDatabase(path, version: _databaseVersion, onCreate: _onCreate); } // SQL code to create the database table Future _onCreate(Database db, int version) async { await db.execute(''' CREATE TABLE $table ( $columnLocationId INTEGER PRIMARY KEY, $columnOrderId INTEGER NOT NULL, $columnCheckinTime TEXT NOT NULL, $columnEventName TEXT NOT NULL ) '''); } // Helper methods Future insert(Map row) async { Database db = await instance.database; return await db.insert(table, row); } Future exists(int locationId) async { Database db = await instance.database; List> rows = await db.query( table, where: '$columnLocationId = ?', whereArgs: [locationId], ); return rows.isNotEmpty; } Future>> queryAllRows() async { Database db = await instance.database; return await db.query(table); } Future>> queryRowsByLocation(int locationId) async { Database db = await instance.database; return await db.query(table, where: '$columnLocationId = ?', whereArgs: [locationId], ); } Future update(Map row) async { Database db = await instance.database; int id = row[columnLocationId]; return await db.update(table, row, where: '$columnLocationId = ?', whereArgs: [id]); } }