在 Flutter 中由用户导入或导出数据库
Import or export database by user in Flutter
我使用 SQFLite 创建了一个数据库。现在我希望用户能够 import/export 他们自己的数据库到应用程序
下面是我在应用中控制某个数据库的方法,大家可以拿去修改:
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
class RoomsDbHelper {
static final RoomsDbHelper _instance = RoomsDbHelper.internal();
factory RoomsDbHelper() => _instance;
RoomsDbHelper.internal();
static Database _db;
Future<Database> createDatabase() async {
print('trying to create 3');
//define the path to the database
String path = join(await getDatabasesPath(), 'rooms.db');
print('trying to create 5');
_db = await openDatabase(path, version: 1, onCreate: (Database db, int v) {
//create all tables
print('trying to create 6');
db.execute(
"create table rooms(id integer , name varchar(50) , roomIcon integer, iconFamily varchar(50), linkIp integer, mID integer, mPass varchar(50),fireSensor integer)");
});
return _db;
}
Future<int> updateRoomName({String newName, int id}) async {
Database db = await createDatabase();
int count = await db.rawUpdate(
'UPDATE rooms SET name = ? WHERE id = ?', ['$newName', '$id']);
print('here in custom ir asasd 22');
return count;
}
Future deleteAllRooms() async {
String path = join(await getDatabasesPath(), 'rooms.db');
try {
await deleteDatabase(path);
print('deleted succesfulyy');
} catch (e) {
print('didnt deleteete $e');
}
}
Future<List> allRooms() async {
Database db = await createDatabase();
return db.query('rooms');
}
Future<List<String>> allNameOfRooms() async {
Database db = await createDatabase();
final List<Map<String, dynamic>> maps =
await db.rawQuery("select name from rooms");
return List.generate(maps.length, (i) {
return maps[i]['name'];
});
}
}
并且每当您想使用此 class 时,只需调用以下任何函数或任何其他函数(您可以修改给定函数以满足您的需要):
RoomsDbHelper helper= RoomsDbHelper();
await helper.createRoom(room);
await _roomsDbHelper.deleteAllRooms();
您需要根据您的应用程序对其进行修改。
A SQLite database is a file in the file system identified by a path. If relative, this path is relative to the path obtained by getDatabasesPath(), which is the default database directory on Android and the documents directory on iOS.
因此,只要您关闭所有数据库连接,您应该能够将此文件复制到某处并从某处获取现有文件并覆盖您的数据库。这是 import/export 完整数据的最简单方法。
如果你想在导入时真正合并数据而不是覆盖,我认为你必须自己编写该逻辑。
我使用 SQFLite 创建了一个数据库。现在我希望用户能够 import/export 他们自己的数据库到应用程序
下面是我在应用中控制某个数据库的方法,大家可以拿去修改:
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
class RoomsDbHelper {
static final RoomsDbHelper _instance = RoomsDbHelper.internal();
factory RoomsDbHelper() => _instance;
RoomsDbHelper.internal();
static Database _db;
Future<Database> createDatabase() async {
print('trying to create 3');
//define the path to the database
String path = join(await getDatabasesPath(), 'rooms.db');
print('trying to create 5');
_db = await openDatabase(path, version: 1, onCreate: (Database db, int v) {
//create all tables
print('trying to create 6');
db.execute(
"create table rooms(id integer , name varchar(50) , roomIcon integer, iconFamily varchar(50), linkIp integer, mID integer, mPass varchar(50),fireSensor integer)");
});
return _db;
}
Future<int> updateRoomName({String newName, int id}) async {
Database db = await createDatabase();
int count = await db.rawUpdate(
'UPDATE rooms SET name = ? WHERE id = ?', ['$newName', '$id']);
print('here in custom ir asasd 22');
return count;
}
Future deleteAllRooms() async {
String path = join(await getDatabasesPath(), 'rooms.db');
try {
await deleteDatabase(path);
print('deleted succesfulyy');
} catch (e) {
print('didnt deleteete $e');
}
}
Future<List> allRooms() async {
Database db = await createDatabase();
return db.query('rooms');
}
Future<List<String>> allNameOfRooms() async {
Database db = await createDatabase();
final List<Map<String, dynamic>> maps =
await db.rawQuery("select name from rooms");
return List.generate(maps.length, (i) {
return maps[i]['name'];
});
}
}
并且每当您想使用此 class 时,只需调用以下任何函数或任何其他函数(您可以修改给定函数以满足您的需要):
RoomsDbHelper helper= RoomsDbHelper();
await helper.createRoom(room);
await _roomsDbHelper.deleteAllRooms();
您需要根据您的应用程序对其进行修改。
A SQLite database is a file in the file system identified by a path. If relative, this path is relative to the path obtained by getDatabasesPath(), which is the default database directory on Android and the documents directory on iOS.
因此,只要您关闭所有数据库连接,您应该能够将此文件复制到某处并从某处获取现有文件并覆盖您的数据库。这是 import/export 完整数据的最简单方法。
如果你想在导入时真正合并数据而不是覆盖,我认为你必须自己编写该逻辑。