Merge pull request #34 from hanxu317317/db

refactor(整理数据库初始逻辑, 判断数据库完整性)
This commit is contained in:
hanxu317317
2019-01-13 19:56:29 +08:00
committed by GitHub

View File

@ -4,37 +4,81 @@ import 'dart:typed_data';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:flutter/services.dart' show rootBundle;
//const createSql = {
// 'cat': """
// CREATE TABLE "cat" (
// `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
// `name` TEXT NOT NULL UNIQUE,
// `depth` INTEGER NOT NULL DEFAULT 1,
// `parentId` INTEGER NOT NULL,
// `desc` TEXT
// );
// """,
// 'collectio': """
// CREATE TABLE collection (id INTEGER PRIMARY KEY NOT NULL UNIQUE, name TEXT NOT NULL, router TEXT);
// """,
// 'widget': """
// CREATE TABLE widget (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, name TEXT NOT NULL, cnName TEXT NOT NULL, image TEXT NOT NULL, doc TEXT, demo TEXT, catId INTEGER NOT NULL REFERENCES cat (id), owner TEXT);
// """;
//};
class Provider {
static Database db;
// 获取数据库中所有的表
Future<List> getTables() async {
if (db == null) {
return Future.value([]);
}
List tables = await db.rawQuery('SELECT name FROM sqlite_master WHERE type = "table"');
List<String> targetList = [];
tables.forEach((item) {
targetList.add(item['name']);
});
return targetList;
}
// 检查数据库中, 表是否完整, 在部份android中, 会出现表丢失的情况
Future checkTableIsRight() async {
List<String> expectTables = ['cat', 'widget', 'collection'];
List<String> tables = await getTables();
for(int i = 0; i < expectTables.length; i++) {
if (!tables.contains(expectTables[i])) {
print("table lost in app");
return false;
}
}
return true;
}
//初始化数据库
// isCreate 用永远 copy 一个新的数据库
Future init(bool isCreate) async {
//Get a location using getDatabasesPath
String databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'flutter.db');
List<Map> tables;
try {
db = await openDatabase(path);
tables = await db
.rawQuery('SELECT name FROM sqlite_master WHERE type = "table"');
print('${tables.length} 7891');
} catch (e) {
print("Error $e");
}
bool tableIsRight = await this.checkTableIsRight();
if (tables.length < 3) {
// Delete the database
await deleteDatabase(path);
if (!tableIsRight) {
// 关闭上面打开的db否则无法执行open
db.close();
// Delete the database
await deleteDatabase(path);
ByteData data = await rootBundle.load(join("assets", "app.db"));
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await new File(path).writeAsBytes(bytes);
db = await openDatabase(path, version: 2,
db = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
print('db created version is $version');
}, onOpen: (Database db) async {
@ -44,4 +88,5 @@ class Provider {
print("Opening existing database");
}
}
}