【问题标题】:Flutter iOS App sqflite database data lost after app upgradeFlutter iOS App sqflite 数据库数据在应用升级后丢失
【发布时间】:2020-10-30 21:27:01
【问题描述】:

我们有一个完全由 Flutter 构建的应用程序。当我们将应用程序升级发送到谷歌商店时,在 Android 设备中更新 apk 时 sqflite 数据库数据不会丢失。 但在 iOS 设备中,用户将应用更新到新版本后,所有数据库数据都会丢失。

颤振 v1.17.5

用于数据持久化的包:

sqflite:^1.3.1 path_provider: ^1.6.11

请您帮忙解决这个问题。应用程序更新到新版本后,不得删除用户数据。也许我需要使用其他类型的数据持久性或者我该如何解决这个问题。

以下是我在 DatabaseHelper 类中初始化数据库的部分:

  class DatabaseHelper {
  static DatabaseHelper _databaseHelper; // Singletone DatabaseHelper
  static Database _database; // Singletone Database

  String wifiSystemTable = 'wifi_system_table';
  String colId = 'id';
  String colDataType = 'data_type';
  String colLocationName = 'location_name';
  String colBackground = 'background';
  String colLocationId = 'location_id';
  String colDeviceType = 'device_type';
  String colIp = 'ip';
  String colName1 = 'name1';
  String colName2 = 'name2';
  String colName3 = 'name3';
  String colRemotePort = 'remote_port';

  DatabaseHelper._createInstance(); // Named constractor to create instance of Database Helper

  factory DatabaseHelper() {
    if (_databaseHelper == null) {
      _databaseHelper = DatabaseHelper
          ._createInstance(); //This is execute only once, singletone object
    }
    return _databaseHelper;
  }

  Future<Database> get database async {
    if (_database == null) {
      _database = await initializeDatabase();
    }
    return _database;
  }

  Future<Database> initializeDatabase() async {
    //Get the directory path for both Android and IOS to store Database
    Directory directory = await getApplicationDocumentsDirectory();
    String path = p.join(directory.toString(), 'wifi.db');

    //Open/create the database at the given path
    var wifiSystemDatabase =
        await openDatabase(path, version: 1, onCreate: _createDb);
    return wifiSystemDatabase;
  }

  void _createDb(Database db, int newVersion) async {
    await db.execute(
        'CREATE TABLE $wifiSystemTable($colId INTEGER PRIMARY KEY, $colDataType INTEGER, $colLocationName TEXT, $colBackground TEXT, $colLocationId INTEGER, $colDeviceType INTEGER, $colIp TEXT, $colName1 TEXT, $colName2 TEXT, $colName3 TEXT, $colRemotePort TEXT)');
  }

非常感谢

【问题讨论】:

  • 我在许多应用程序中使用 SQLite 从未遇到过这个问题。如果您可以在初始化数据库的位置显示代码的某些部分,那将会很有帮助。
  • 感谢@AjayKumar 的回复。但是你是否在使用 sqflite 包时使用它?
  • @AjayKumar 我编辑了我的问题并添加了您要求的一些部分。你能看看吗。谢谢

标签: ios database flutter sqflite


【解决方案1】:

我认为您的数据库路径正在发生变化。试试这个。

  Future<Database> initializeDatabase() async {
    //Get the directory path for both Android and IOS to store Database
    String databasesPath = await getDatabasesPath();
    String path = p.join(databasesPath, 'wifi.db');
    //Open/create the database at the given path
    var wifiSystemDatabase = await openDatabase(path, version: 1, onCreate: _createDb);
    return wifiSystemDatabase;
  }

希望对你有帮助:)

【讨论】:

  • 谢谢大家,它像魔术一样解决了我的问题。我非常感谢您的支持。你是最棒的!!!
  • 我有同样的问题,当我升级了我的颤振版本并升级了我的 pubspec.yaml 中的 sdk 版本并且我在我的手机中运行该应用程序时,在 vscode 终端中它说类似'卸载旧版本'。安装完成后,我在安卓手机中的所有sql数据都被删除了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-20
  • 1970-01-01
  • 2021-07-09
  • 2018-11-10
  • 1970-01-01
  • 2021-10-12
相关资源
最近更新 更多