栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

有没有办法使用sqflite在flutter应用程序中检查数据库版本?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

有没有办法使用sqflite在flutter应用程序中检查数据库版本?

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

    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 dbdb = 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;      }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/413804.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号