views:

1614

answers:

1

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

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

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

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.

Thanks for you answers!

+3  A: 

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.

Josef
Thanks for the reply Josef, I have tried to ship the sqlite3 .db file in the assets folder of the .apk (like reigndesign post outlines). The issue I'm having with the reigndesign solution is that the onUpgrade() does not get called when my database version # changes. In fact, the only way I can get it to replace the existing database with a newer version is to work around the onUpgrade() and add a statement that explicitly deletes the database just before the createDataBase() is called (which basically forces you to recreate the database every time the Activity is run... =(
MoMo
As for using transactions that may be a workable solution. I was able to get the onUpgrad() to be call consistency when I implemented this solution (http://www.screaming-penguin.com/node/7742) but a simple sql import statement is still ideal considering the upgrade and tweaks necessary to get the reigndesign solution to work (i.e. the '_id' column and the 'android_metadata' table). While the tweaks are not difficult to implement I fear it adds unnecessary dependencies to how the Android platform expects their databases to be created. If that were ever to change so would our solution...
MoMo