有什么办法以Flutter方式将CSV文件导入Sqlite数据库吗? [英] Is there any way to Import CSV file to Sqlite Database in Flutter way?
问题描述
我一直在寻找以波动的方式将csv导入数据库。但不幸的是,我找不到与此案有关的文章。所以我想知道是否有一种方法可以将csv导入数据库。如果是,我该如何实现?就我而言,我打开文件选择器,并想要导入csv文件。我可以轻松地以Kotlin方式进行操作,但不能以颤抖的方式进行操作。我将不胜感激。
I have been searching to import csv to database in flutter way. But unfortunately, I cannot find articles related to this case. So I wonder if there is a way to import csv to database. If yes, how can I achieve that? For my case, I open the file picker and want to import csv file. I can easily do it in Kotlin way but not in a flutter way. I would appreciate any help.
推荐答案
嗯,这并不简单。
您通常会使用 https:// pub.dev/packages/csv 或 https://pub.dev/packages/spreadsheet_decoder以便解析CSV。
You'd normally use something like https://pub.dev/packages/csv or https://pub.dev/packages/spreadsheet_decoder in order to parse CSV.
所以它看起来像:
final input = new File('documents/file.csv').openRead();
然后将其转换为列表:
final fields = await input.transform(utf8.decoder).transform(new CsvToListConverter()).toList();
完成此操作后,通常会有一个要执行的功能批量更新。
After you did that, you'd normally have a function that is going to do a bulk update.
首先,创建一个数据库
Database database = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
await db.execute(
'CREATE TABLE Test (id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)');
});
然后您可以使用此处提到的一些帮助方法=> https://stackoverflow.com/a/56507307/1737811 ,以便使用您的结果填充数据库字段。
Then you could use some helper method like mentioned here => https://stackoverflow.com/a/56507307/1737811 in order to populate the database fields with your result.
那样,您将传递您的表名
,当然还有您的列表
包含刚解码的CSV中的值。
That way you'd pass your tablename
, and of course your List
containing the values from the CSV that you've just decoded.
这篇关于有什么办法以Flutter方式将CSV文件导入Sqlite数据库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!