我正在使用Ffltter SQflite来存储Ffltter中的数据,到目前为止,我还没有遇到任何问题。现在我想用它更新我的应用程序和数据库。到目前为止,我只是用新的数据库替换旧的数据库。但是现在有一些表我想保存在那里的数据(用户数据)。我怎样才能更换一些table并保留另一些table呢?(表结构到目前为止还没有改变)编辑:我正在使用SQlite的DB浏览器在Ffltter外部创建和填充数据库(当然,除了应用程序数据中的用户)
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);
}
2条答案
按热度按时间xe55xuns1#
这是一个迁移的例子。
在下面的代码中,“OpenDatabase”方法的顺序如下:
基于Medium文章,但没有使用“SQLite_Migration”包
在本例中,我正在初始化一个USERS表,该表包含一个id和first_name列。
现在,如果我想添加一个LAST_NAME列,我只需在“MigrationScript”Map中添加SQL查询。
编辑:多表用例
nbysray52#
查看this medium文章,它贯穿了如何设置迁移,作者甚至创建了一个package来清理整个过程。它有点旧了,但应该还能挺得住。我在设置我的应用程序时使用了它,这款应用程序已经使用了3个月。
这个插件应该可以帮助你将创建/修改/删除任何你想要的表的SQL查询组织成可以在数据库初始化时运行的迁移。我建议从在DB浏览器中创建数据库转向使用SQL查询,就像本文所做的那样,将所有东西都保存在一个地方。