有没有办法使用sqflite在flutter应用程序中检查数据库版本? [英] Is there a way to check the database version in a flutter app using sqflite?

查看:136
本文介绍了有没有办法使用sqflite在flutter应用程序中检查数据库版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是扑朔迷离的新手,我对使用sqflite的这些应用程序如何进行数据库版本控制有一些疑问。

I am pretty new to flutter and I have some questions about how database version control works for these apps with sqflite.

我已经在我的资产文件夹中创建了一个数据库文件,该文件在启动应用程序时复制到用户的设备上。我想知道是否有任何方法可以检查用户数据库文件的当前版本,从而不必每次都复制它?

I have made a database file in my assets folder that is copied onto the device of the user when the app is launched. I am wondering if there is any way to check the current version the user has of the database file, so that it does not have to be copied over every time?

案例:
我对Google Play商店中随应用程序更新一起提供的数据库文件进行了更新。
是否可以检查用户当前拥有的数据库文件的版本,以确定是否应使用新文件替换旧文件?

Case: I make an update to the database file that is shipped with an app update in the google play store. Can the version of the database file that the user currently has, be checked to determine if a new file should replace the old one?

如果是这样,每次更新应用程序时都可以进行此检查吗?每次打开应用程序时都需要这样做吗?

If so... Can I do this check every time I update my app? Do I have to do it every time the app is opened?

如果没有,该如何处理?处理用户设备上存储的文件的数据库更新的最佳方法是什么?

If not.. How is this best handled? What is the best way of handling database updates of files stored on a users device?

推荐答案

SQFlite已经具有内置版本管理,但只有在具有迁移脚本的情况下,它才有效。对于您所说的,可能不是您的情况。您需要手动管理版本控制:

SQFlite has already a built-in version management, but it will work only if you have a migration script. For what you've said that's probably not your case. You'll need to manage your version control by hand:

    class DbHelper {
      static const NEW_DB_VERSION = 2;

      static final DbHelper _instance = DbHelper.internal();

      factory DbHelper() => _instance;

      DbHelper.internal();

      Database _db;

      Future<Database> get db async {
        if (_db != null) {
          return _db;
        } else {
          _db = await initDb();
          return _db;
        }
      }

      Future<Database> initDb() async {

        final databasesPath = await getDatabasesPath();
        final path = join(databasesPath, "database.db");

        var db = await openDatabase(path);

        //if database does not exist yet it will return version 0
        if (await db.getVersion() < NEW_DB_VERSION) {

          db.close();

          //delete the old database so you can copy the new one
          await deleteDatabase(path);

          try {
            await Directory(dirname(path)).create(recursive: true);
          } catch (_) {}

          //copy db from assets to database folder
          ByteData data = await rootBundle.load("assets/databases/database.db");
          List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
          await File(path).writeAsBytes(bytes, flush: true);

          //open the newly created db 
          db = await openDatabase(path);

          //set the new version to the copied db so you do not need to do it manually on your bundled database.db
          db.setVersion(NEW_DB_VERSION);

        }

        return db;
      }
    }

这篇关于有没有办法使用sqflite在flutter应用程序中检查数据库版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆