繁体   English   中英

Flutter iOS 应用升级后应用 sqflite 数据库数据丢失

[英]Flutter iOS App sqflite database data lost after app upgrade

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

flutter v1.17.5

Package 用于数据持久化:

sqflite:^1.3.1 path_provider:^1.6.11

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

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

  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)');
  }

非常感谢

我认为您的数据库路径正在改变。 尝试这个。

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

希望能帮助到你:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM