【发布时间】:2020-05-12 10:42:49
【问题描述】:
我正在使用flutter SQflite将数据存储在flutter中,到目前为止我没有任何问题。现在我想用它更新我的应用程序和数据库。到目前为止,我只是用新数据库替换旧数据库。但现在有一些表我想保留数据(用户数据)。我怎样才能更换一些桌子并保留其他桌子? (到目前为止,表结构没有变化) 编辑:我正在使用用于 SQlite 的 DB 浏览器在 Flutter 之外创建和填充数据库(当然,应用程序数据中的用户除外)
Future<void> initDatabase() async {
var databasesPath = await getDatabasesPath();
var path = join(databasesPath, "recipes.db");
// Check if the database exists
var exists = await databaseExists(path);
if (!exists) {
// Should happen only the first time you launch your application
print("Creating new copy of database from asset");
// Make sure the parent directory exists
try {
await Directory(dirname(path)).create(recursive: true);
} catch (_) {}
// Copy from asset
ByteData data = await rootBundle.load(join("assets", "recipes.db"));
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
// Write and flush the bytes written
await File(path).writeAsBytes(bytes, flush: true);
} else {
print("Opening existing database");
}
// open the database
db = await openDatabase(path,version: 2, readOnly: false);
}
【问题讨论】:
标签: database sqlite flutter migration sqflite