You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
travelguideProject/src/lib/travelguideapp/data/MyDatabase.dart

44 lines
1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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);
}
}