SQLite扑朔迷离,数据库资产如何工作 [英] Sqlite in flutter, how database assets work
问题描述
我正在查看此( https://github.com/tekartik/sqflite/blob/master/doc/opening_asset_db.md )用于填充已格式化且需要应用程序的数据,仅用于读取功能.
I am looking at this (https://github.com/tekartik/sqflite/blob/master/doc/opening_asset_db.md) for populating data that is already formatted and need for the app, for read functionality only.
因此,当我已经在外部csv文件中拥有所有信息时,我对创建sqlite数据库的理解是,在我的应用程序的.dart文件中创建类模型,例如
So my understanding of creating a sqlite database when we already have all the information in an outside csv file is to, create the class models in a .dart file in my app, such as
class User {
int id;
String _firstName;
String _lastName;
String _dob;
User(this._firstName, this._lastName, this._dob);
User.map(dynamic obj) {
this._firstName = obj["firstname"];
this._lastName = obj["lastname"];
this._dob = obj["dob"];
}
String get firstName => _firstName;
String get lastName => _lastName;
String get dob => _dob;
Map<String, dynamic> toMap() {
var map = new Map<String, dynamic>();
map["firstname"] = _firstName;
map["lastname"] = _lastName;
map["dob"] = _dob;
return map;
}
void setUserId(int id) {
this.id = id;
}
}
然后,如果我有一个包含所有用户信息的csv文件(其值与用户类相对应),是否可以使用数据库资产填写该信息,然后在其中调用扑扑的应用程序?我意识到可能有很多方法可以解决此问题,但是.db文件到底存储了什么,以及它的格式如何?我可以在此.db文件中实现.csv文件吗?
then if i have a csv file with all the user information inside of it (with values that correspond to the user class), could i be using the database asset to have that fill out the information and then call to it inside of the flutter app? I realize there are probably many ways to go about this, but What exactly is the .db file storing, and how is it formatted? Can i implement a .csv file into this .db file?
推荐答案
首先,您需要从csv构建一个sqlite数据库.这可以通过以下方式完成:
First off, you will need to construct a sqlite database from your csv. This can be done in the following way:
-
创建必要的表(users.sql)
Create the necessary table (users.sql)
CREATE TABLE users(
firstname TEXT NOT NULL,
lastname TEXT NOT NULL,
dob TEXT NOT NULL
);
创建sqlite数据库
Create the sqlite database
sqlite3 database.db < users.sql
插入csv数据
Insert the csv data
sqlite3 database.db
.mode csv
.import data.csv users
将database.db放入您的资产中,并将其添加到pubspec.yaml中.
Put database.db into your assets and add that in pubspec.yaml.
flutter:
# ...
assets:
- assets/database.db
在您的应用中,您必须将资产文件复制到文档"中.这有点复杂.
In your app, you'll have to copy the asset file into "documents". This is slightly complicated.
// Construct a file path to copy database to
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, "asset_database.db");
// Only copy if the database doesn't exist
if (FileSystemEntity.typeSync(path) == FileSystemEntityType.notFound){
// Load database from asset and copy
ByteData data = await rootBundle.load(join('assets', 'database.db'));
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
// Save copied asset to documents
await new File(path).writeAsBytes(bytes);
}
最后,您可以像这样访问数据库.
Lastly, you can access the database like so.
Directory appDocDir = await getApplicationDocumentsDirectory();
String databasePath = join(appDocDir.path, 'asset_database.db');
this.db = await openDatabase(databasePath);
initialized = true;
示例查询(this._initialize()是步骤6)
Example query (this._initialize() is step 6)
Future<List<Page>> search(String word, int parentId) async {
if (!initialized) await this._initialize();
String query = '''
SELECT * FROM users
LIMIT 25''';
return await this.db.rawQuery(query);
}
这篇关于SQLite扑朔迷离,数据库资产如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!