integrated with previous
This commit is contained in:
82
lib/utils/checkin_db_helper.dart
Normal file
82
lib/utils/checkin_db_helper.dart
Normal file
@ -0,0 +1,82 @@
|
||||
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<Database> 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<int> insert(Map<String, dynamic> row) async {
|
||||
Database db = await instance.database;
|
||||
return await db.insert(table, row);
|
||||
}
|
||||
|
||||
Future<bool> exists(int locationId) async {
|
||||
Database db = await instance.database;
|
||||
List<Map<String, dynamic>> rows = await db.query(
|
||||
table,
|
||||
where: '$columnLocationId = ?',
|
||||
whereArgs: [locationId],
|
||||
);
|
||||
return rows.isNotEmpty;
|
||||
}
|
||||
|
||||
Future<List<Map<String, dynamic>>> queryAllRows() async {
|
||||
Database db = await instance.database;
|
||||
return await db.query(table);
|
||||
}
|
||||
|
||||
Future<List<Map<String, dynamic>>> queryRowsByLocation(int locationId) async {
|
||||
Database db = await instance.database;
|
||||
return await db.query(table,
|
||||
where: '$columnLocationId = ?',
|
||||
whereArgs: [locationId],
|
||||
);
|
||||
}
|
||||
|
||||
Future<int> update(Map<String, dynamic> row) async {
|
||||
Database db = await instance.database;
|
||||
int id = row[columnLocationId];
|
||||
return await db.update(table, row, where: '$columnLocationId = ?', whereArgs: [id]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user