-2

I created sqlite database table and i would like to populate the database table with a CSV file saved in the sd card. my question is, is there any method or approach to insert the entire csv file into the sqlite database at once instead of inserting it line by line?

Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • where is your csv file in sdcard or assets folder – Naveen Tamrakar Feb 25 '16 at 09:54
  • yes first u read csv file using csvreader in android than u insert data in database there is not any directed way to read and save data in database – Naveen Tamrakar Feb 25 '16 at 10:04
  • This link appears to cover what you are asking [Is it possible to insert multiple rows at a time in an SQLite database?](http://stackoverflow.com/questions/1609637/is-it-possible-to-insert-multiple-rows-at-a-time-in-an-sqlite-database) – MikeT Feb 25 '16 at 10:05

1 Answers1

0

This is not possible right away unless you use a tool like SQLiteDatabaseBrowser:

enter image description here

If this is not working for you, you can follow the following steps to implement your own import:

  1. open the file with FileOutputStream to read its contents
  2. read the first line (headers) and STring.split() items by ,
  3. build an insert SQL String based on the items/headers
  4. create a PreparedStatement from it
  5. read all subsequent lines and String.split() values by ,
  6. assign bind variables
  7. execute your PreparedStatement

It's not very difficult and if you don't need the right or best fitting data type that's all.

Good luck!

Trinimon
  • 13,839
  • 9
  • 44
  • 60