木鱼配置记录-数据库
This commit is contained in:
47
lib/storage/db_storage/dao/merit_record_dao.dart
Normal file
47
lib/storage/db_storage/dao/merit_record_dao.dart
Normal file
@@ -0,0 +1,47 @@
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import '../../../muyu/models/merit_record.dart';
|
||||
|
||||
class MeritRecordDao {
|
||||
final Database database;
|
||||
|
||||
MeritRecordDao(this.database);
|
||||
|
||||
static String tableName = 'merit_record';
|
||||
|
||||
static String tableSql = """
|
||||
CREATE TABLE $tableName (
|
||||
id VARCHAR(64) PRIMARY KEY,
|
||||
value INTEGER,
|
||||
image TEXT,
|
||||
audio TEXT,
|
||||
timestamp INTEGER
|
||||
)""";
|
||||
|
||||
static Future<void> createTable(Database db) async{
|
||||
return db.execute(tableSql);
|
||||
}
|
||||
|
||||
Future<int> insert(MeritRecord record) {
|
||||
return database.insert(
|
||||
tableName,
|
||||
record.toJson(),
|
||||
conflictAlgorithm: ConflictAlgorithm.replace,
|
||||
);
|
||||
}
|
||||
|
||||
Future<List<MeritRecord>> query() async {
|
||||
List<Map<String, Object?>> data = await database.query(
|
||||
tableName,
|
||||
);
|
||||
|
||||
return data
|
||||
.map((e) => MeritRecord(
|
||||
e['id'].toString(),
|
||||
e['timestamp'] as int,
|
||||
e['value'] as int,
|
||||
e['image'].toString(),
|
||||
e['audio'].toString(),
|
||||
))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
35
lib/storage/db_storage/db_storage.dart
Normal file
35
lib/storage/db_storage/db_storage.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import 'dao/merit_record_dao.dart';
|
||||
|
||||
|
||||
class DbStorage {
|
||||
DbStorage._();
|
||||
|
||||
static DbStorage? _storage;
|
||||
|
||||
static DbStorage get instance {
|
||||
_storage = _storage ?? DbStorage._();
|
||||
return _storage!;
|
||||
}
|
||||
|
||||
late Database _db;
|
||||
|
||||
late MeritRecordDao _meritRecordDao;
|
||||
MeritRecordDao get meritRecordDao => _meritRecordDao;
|
||||
|
||||
Future<void> open() async {
|
||||
String databasesPath = await getDatabasesPath();
|
||||
String dbPath = path.join(databasesPath, 'first_station.db');
|
||||
_db = await openDatabase(dbPath, version: 1, onCreate: _onCreate);
|
||||
_meritRecordDao = MeritRecordDao(_db);
|
||||
}
|
||||
|
||||
void _onCreate(Database db, int version) async {
|
||||
await MeritRecordDao.createTable(db);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user