I am writing an android app. I have a txt file with values I need to put in an sqlite database. How can this be done?
Asked
Active
Viewed 623 times
-2
-
Go through the file and run insert on every line. If you're using the SQLiteOpenHelper class, do this in onCreate – Gabe Sechan Mar 26 '13 at 19:13
-
4There's no automated way. You will have to write your own parser and insert the values in the db. You will not get a usable answer, since the specific txt file is not included and your question is too vague for people to spend time – Entreco Mar 26 '13 at 19:14
-
In addition to @Gabe's comment, you should put the .txt file in the assets sub-directory of your project. – Code-Apprentice Mar 26 '13 at 19:14
-
1This is too general of a question. Try to write a parser that will read the text file and research how to use SQLite. Come back and ask a *specific* question with where you exactly get stuck. – boltup_im_coding Mar 26 '13 at 20:28
-
THIS IS NOT AN EXACT DUPLICATE OF THE EXISTING QUESTION IN THE LINK PROVIDED. The link points to a USELESS Q and A. WHO DID THIS?? – DSlomer64 Sep 15 '17 at 19:05
1 Answers
0
Some points:
- Include the .txt file as an asset in your app. This will allow you to open it with an AssetFileDescriptor.
- You have to read data from the .txt file and insert rows into your database. It's easier to do this if you have some control over the format of your .txt file. For example, if you can you should insert commas between the values, and insert a line feed at the end of each row's worth of data.
- Use transactions when necessary.
- Do the operation on a background thread. I suggest using an IntentService.

Joe Malin
- 8,621
- 1
- 23
- 18