我正在看这个(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 an 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);
}
这篇关于Flutter 中的 Sqlite,数据库资产如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!