fix bug:android emulator

This commit is contained in:
yifeng.yl
2019-01-10 11:13:13 +08:00
parent 71292363a3
commit dad3e040d4
5 changed files with 28 additions and 14 deletions

View File

@ -11,27 +11,38 @@ class Provider {
//初始化数据库
// isCreate 用永远 copy 一个新的数据库
Future init(bool isCreate) async {
//Get a location using getDatabasesPath
String databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'flutter.db');
try {
db = await openDatabase(path);
} catch (e) {
print("Error $e");
}
if (isCreate) {
List<Map> list;
// try {
// db = await openDatabase(path, readOnly: true);
// } catch (e) {
// print("Error $e");
// }
if (db == null) {
// 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,
onCreate: (Database db, int version) async {
print('db created version is $version');
}, onOpen: (Database db) async {
print('new db opened');
});
// db = await openDatabase(path, version: 2,
// onCreate: (Database db, int version) async {
// print('db created version is $version');
// }, onOpen: (Database db) async {
// print('new db opened');
// });
db = await openDatabase(path, readOnly: true);
list = await db
.rawQuery('SELECT name FROM sqlite_master WHERE type = "table"');
print('$list 1234567891');
} else {
// print('Opening existing database');
print("Opening existing database");
}
}
}