|
|
import 'package:sqflite/sqflite.dart';
|
|
|
import 'package:path/path.dart';
|
|
|
import 'dart:async';
|
|
|
|
|
|
class MyDatabase {
|
|
|
static Future<Database> initDatabase() async {
|
|
|
final databasePath = await getDatabasesPath();
|
|
|
final path = join(databasePath, 'travelguide.db');
|
|
|
// 打开数据库连接
|
|
|
//onCreate表示只有如果数据库版本发生变化,将会触发 onCreate 回调
|
|
|
final database =
|
|
|
await openDatabase(path, version: 1, onCreate: _createTables);
|
|
|
|
|
|
await _createTables(database, 1);
|
|
|
|
|
|
return Future.value(database);
|
|
|
}
|
|
|
|
|
|
static Future<void> _createTables(Database db, int version) async {
|
|
|
await _createCollectionInfoTable(db, version);
|
|
|
}
|
|
|
|
|
|
static Future<void> _createCollectionInfoTable(Database db, int version) async {
|
|
|
await db.execute('''
|
|
|
CREATE TABLE IF NOT EXISTS CollectionInfo(
|
|
|
CollectionID BIGINT PRIMARY KEY,
|
|
|
MuseumName TEXT NOT NULL,
|
|
|
CollectionName TEXT NOT NULL,
|
|
|
CollectionDetail TEXT NOT NULL
|
|
|
);
|
|
|
''');
|
|
|
print("CollectionInfoTable create success");
|
|
|
}
|
|
|
|
|
|
static Future<void> reBuildDatabase() async {
|
|
|
final databasePath = await getDatabasesPath();
|
|
|
final path = join(databasePath, 'tma.db');
|
|
|
|
|
|
final database =
|
|
|
await openDatabase(path, version: 1, onCreate: _createTables);
|
|
|
await _createTables(database, 1);
|
|
|
}
|
|
|
}
|