【发布时间】: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