从 CSV 文件填充 Android 数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2887119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Populate Android Database From CSV file?
提问by MoMo
Is it possible to take a csv file stored in the res/raw resource directory and use it to populate a table in the sqlite3 database?
是否可以将存储在 res/raw 资源目录中的 csv 文件用于填充 sqlite3 数据库中的表?
My thought was that, if there was a way to do a bulk import for the entire file into the table then that would be cleaner and faster than iterating over each line in the file and executing individual insert statements...
我的想法是,如果有一种方法可以将整个文件批量导入到表中,那么这将比遍历文件中的每一行并执行单个插入语句更清晰、更快......
I've found that there is a sqlite import command that allows this: How to import load a .sql or .csv file into SQLite?
我发现有一个 sqlite 导入命令允许这样做:How to import load a .sql or .csv file into SQLite?
...but I'm having trouble applying those statements in my Android application. My first thought was to try something like the following...but no luck:
...但我无法在我的 Android 应用程序中应用这些语句。我的第一个想法是尝试类似以下的东西......但没有运气:
db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT)");
db.execSQL(".mode csv");
db.execSQL(".import res/raw/MyFile.csv " + TABLE_NAME);
Is this possible?
这可能吗?
Should I be trying a different approach to populate my database?
我应该尝试不同的方法来填充我的数据库吗?
UPDATE: I'm marking Josef's response as the answer (bulk insert using transactions) because it works fine and directly answers my question based on my title (thanks Josef). However, I'm am still looking for a way to do a bulk insert in an Android app from csv file into a sqlite3 table using the import statement. If you know how to do this please respond.
更新:我将 Josef 的回复标记为答案(使用事务批量插入),因为它工作正常并根据我的标题直接回答我的问题(感谢 Josef)。但是,我仍在寻找一种使用 import 语句在 Android 应用程序中从 csv 文件批量插入 sqlite3 表的方法。如果您知道如何执行此操作,请回复。
Thanks for you answers!
谢谢你的回答!
回答by Josef Pfleger
If you want to package static data with your application, I recommend preparing the database at development time (using any UI or csv-import command you like) and shipping the sqlite file inside the assets folder. You can then simply copy the entire sqlite file onto the device when your application is first run. Thesepoststake you through this idea which is most likely the fastest way to setup a database (file copy speed).
如果你想用你的应用程序打包静态数据,我建议在开发时准备数据库(使用任何你喜欢的 UI 或 csv-import 命令)并将 sqlite 文件传送到资产文件夹中。然后,当您的应用程序首次运行时,您可以简单地将整个 sqlite 文件复制到设备上。这些帖子将带您了解这个想法,这很可能是设置数据库的最快方法(文件复制速度)。
If, for some reason you do need to insert a lot of data at run time, I recommend you look at ways to bulk insert using transactionsto speed it up.
回答by Opy
I spent a lot of time looking for a simple solution and came up with this little bit of code myself. For future people looking for this like I was you can use this:
我花了很多时间寻找一个简单的解决方案,并自己想出了这一点代码。对于像我一样寻找这个的未来人,你可以使用这个:
BufferedReader in = new BufferedReader(your csv file source here);
String reader = "";
while ((reader = in.readLine()) != null){
String[] RowData = reader.split(",");
date = RowData[0];
value = RowData[1];
ContentValues values = new ContentValues();
values.put(CsvProvider.DATE, date);
values.put(CsvProvider.VALUE, value);
getContentResolver().insert(CsvProvider.CONTENT_URI, values);
}
in.close();
My CSV
file has no titles and only has 2 columns but more than two columns should not be a problem. Just remember to specify what is splitting your columns and for each column add another RowData[#]
(you have to start with 0). You want to make sure whatever you are going to do with each line is done before you call in.close()
. I am using a content provider but you can really do whatever you want with the data like append it to a String[]
or whatever else.
我的CSV
文件没有标题,只有 2 列,但超过两列应该不成问题。只需记住指定拆分列的内容,并为每一列添加另一个列RowData[#]
(您必须从 0 开始)。您想确保在调用 之前完成每行的任何操作in.close()
。我正在使用一个内容提供者,但你真的可以对数据做任何你想做的事情,比如将它附加到 aString[]
或其他任何东西。
While I am using an input stream you can point the BufferedReader to wherever you want. As long as the BufferedReader
can read it then it will work.
当我使用输入流时,您可以将 BufferedReader 指向您想要的任何位置。只要BufferedReader
可以读取它然后它就会工作。