0

Possible Duplicate:
Bulk Insertion on Android device

So i want to add about 300 entries to a sqlite database table for my android app. I want to do this fast and efficiently without a noticeable delay for the users. I have a list of itmes on hand that i want to add (THIS LIST WILL NOT DYNAMICALLY CHANGE). Should i make an arryalist and then iterate through that list to add the entries or should i run a method like this for every entry string that i want to add?:

public long creatSQLEntry(String name){
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_STUFF, name);
    return myDatabase.insert(MY_Table, null, cv);
}

Im thinking that the above code isn't the most efficient way to do this. Anyone know how to do this? some sample code would be awesome but is not a must.

Community
  • 1
  • 1
Goku
  • 1,565
  • 1
  • 29
  • 62
  • 1
    Will the data be the different for every user? Will you retrieve it from the Internet and insert it after doing so? If you answered no on both questions, consider passing a pre-filled database with your app and just move it into the correct location. – karllindmark Nov 15 '12 at 22:52
  • NinetwoZero, where is some documentation on how this is done correctly and easily? – Goku Nov 15 '12 at 22:55

3 Answers3

2

Compile the statements and use a transaction to insert all the rows programmatically. I do this for a static database and it is very quick.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
1

You can prepare the db on your desktop then copy the db over to the private database folder on the initial load.

http://zaman91.wordpress.com/2010/09/22/android-how-to-use-own-sqlite-database/

Rejinderi
  • 11,694
  • 2
  • 31
  • 40
  • I think this is a hackish solution, that could easily break if android_metadata or database path happens to vary. I would not recommend this. – Emil Davtyan Nov 15 '12 at 23:52
0

If this data list is constant, I would have another look at the database design.

gssi
  • 5,043
  • 5
  • 23
  • 23