0

I am currently learning how to use database in Android. For one of my app, I need to stock 500 string values in order to read them. Actually to fill my database, I read a .txt file at the database creation with all the string values inside. For each lines of .txt (corresponding to a string value), I create a new data in the database. The thing is I know that accessing a file can be long it is not a very good thing if there are thousands of values. According to that, I have two questions :

  • Is it really usefull to create a database ? (because I can directly access the string values by reading the .txt)

  • Is there a better way to fill the database ? Or a way to create a database already filled before the activity creation ? (because currently, each time you close the activity you close the database and each time you reopen it, it recreate and refill the database.)

Mtoypc
  • 464
  • 1
  • 6
  • 24
  • It's possible to initially fill an SQlite database from a textfile and then distribute the filled SQlite database with your app. No need to include the .txt file in your app. – Frank D. Sep 01 '15 at 08:17
  • Do you heard about sync data using webservice / – Sree Sep 01 '15 at 08:17
  • Frank D. that's what I want to do but in all tutorials I saw, the database creation takes place in the app's code. Putting an already existing database would be exactly what I need but I have no idea of how to do it. Is that in another project that I should fill it ? – Mtoypc Sep 01 '15 at 08:19
  • `Or a way to create a database already filled before the activity creation ?` Yes, you can provide a pre-filled database to your app. – Phantômaxx Sep 01 '15 at 08:22
  • 1
    It's worth checking out SQLiteBrowser. It allows you to create an .sqlite file from your desktop which you can copy to the assets folder in your app. http://sqlitebrowser.org/ – Frank D. Sep 01 '15 at 09:21

1 Answers1

1

How about instead of having .txt have a .csv file with values and use org.apache.commons.csv or super csv for parsing the csv file. I haven't used super csv but it provides support for POJO support which i think can be really handy. This will increase your performance and parsing speed. But it would be based on your situation. i would recommend creating a database if you need to perform SQL queries on your data for eg. joins or nested queries. If you just need to display you can use a CSV file.

harshitpthk
  • 4,058
  • 2
  • 24
  • 32