0

Basically, I'm trying to store some data (~300 rows, ~10 columns) for an android app. This data will not be changed by the app, just used in calculations and stuff.

However, everything I've found online (example) talks about using a database that is created at runtime. I do not want this, the data will be the same every time the app is run.

So is there a way of using a databse like this? (Or any other way of storing data in a table-like fashion?)

ACarter
  • 5,688
  • 9
  • 39
  • 56
  • 1
    this may be a duplicate of http://stackoverflow.com/questions/2409126/android-pre-populated-database . – k3b Feb 27 '12 at 19:23
  • Thanks. That question had a link to another question which had a link to a blog which helped. – ACarter Feb 27 '12 at 20:49
  • (Sorry if you saw my previous comment, I was going to edit it, but seemingly I can't. If you didn't, ignore me.) – ACarter Feb 27 '12 at 20:53

3 Answers3

2

Generate the SQLite database as part of your build and keep it in your app's raw resources. Since all you need is the file's path and name to open it, you can still read it fine. Your open helper will still go through onCreate() the first time unless you include the table Android uses for its own bookkeeping, but that should be okay.

Make sure you only open it for reading, and you should be good to go.

Argyle
  • 3,324
  • 25
  • 44
1

How about storing this data in raw file under Assets or Res/raw folder. You can either dump this data on the fly in Database or read it and process it [which may be costly]. Dynamic handling may be costly, test it and compare performance.

kodeshpa
  • 549
  • 5
  • 14
  • I would recommend using the assets folder instead of the res. in this way he can directly access the files using the Java File api. – Rafael Sanches Feb 27 '12 at 20:41
1

Put your custom file in the assets folder under the project root.

To get a inputstream from the file, just do:

context.getAssets().open(file);

In this way you can store your static data in conma separated or any model you want.

If you want the data constantly changing, you can create a temporary file in the SDCard, by accessing and creating a new file under some path at:

Environment.getExternalStorageDirectory()
Rafael Sanches
  • 1,823
  • 21
  • 28
  • So basically, I have a plain text file. In this file, I separate the data with something, and then can get that data using some sort of find-and-replace. Sounds great, thanks. – ACarter Feb 27 '12 at 20:51
  • 1
    you can simply load the data in a string and then make string.split("\n")... for (String row : lines) { String[] cols = row.split(",") } it will work probably fine for your needs. – Rafael Sanches Feb 28 '12 at 02:49