填充Android的数据库从CSV文件? [英] Populate Android Database From CSV file?

查看:163
本文介绍了填充Android的数据库从CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能采取存储在res /原始资源目录csv文件,并用它来填充一个表中的sqlite3的数据库?

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?

我的想法是,如果有一种方法可以做到批量导入整个文件到表那将是比遍历每行的文件中,并执行单独的插入语句更清洁,更快速...

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...

我发现有一个SQLite导入命令,允许这样的:<一href="http://stackoverflow.com/questions/1045910/how-can-i-import-load-a-sql-or-csv-file-into-sqlite">http://stackoverflow.com/questions/1045910/how-can-i-import-load-a-sql-or-csv-file-into-sqlite

I've found that there is a sqlite import command that allows this: http://stackoverflow.com/questions/1045910/how-can-i-import-load-a-sql-or-csv-file-into-sqlite

...但我有申请在我的Andr​​oid应用这些语句的麻烦。我首先想到的是去尝试像以下...但没有运气:

...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:

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); 

这可能吗?

我应该尝试不同的方法来填充我的数据库?

Should I be trying a different approach to populate my database?

更新:我打约瑟夫的反应是答案(大部分使用插入交易),因为它工作正常,并直接回答根据我的头衔(感谢约瑟夫)我的问题。不过,我现在还在找工作的方式使用import语句做批量插入从CSV文件中的Andr​​oid应用程序到sqlite3的表。如果你知道如何做到这一点请回复。

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.

感谢您的解答!

推荐答案

如果你想与你的应用程序打包的静态数据,我建议preparing数据库在开发时(使用任何你喜欢的UI或CSV导入命令)和运输内的资产文件夹中的sqlite的文件。然后,您可以将整个源码文件简单地复制到设备上,当你的应用程序第一次运行。 这些 <一href="http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/">posts带你穿越这个想法是最有可能以最快的方式建立一个数据库(文件复制速度)。

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. These posts take you through this idea which is most likely the fastest way to setup a database (file copy speed).

如果由于某种原因,你确实需要在运行时插入大量的数据,我建议你看一下的方式来的使用事务批量插入来加速这一过程。

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 transactions to speed it up.

这篇关于填充Android的数据库从CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆