-1

I'm developing a little Android app which is going to be offline, and I need to store the SQLite base's data into a file, that I want to put into the apk's resources, I could use XML or JSON files, but I was wondering if there weren't a better solution.

Thanks in advance :)

ArteFact
  • 517
  • 4
  • 14
  • `.db` I think :-) All `SQLite` files has an extension `.db` – Aleksandr Nov 03 '16 at 09:59
  • I meant I want to store data, not the base itself ^^ I'll parse the file to fill the base – ArteFact Nov 03 '16 at 10:02
  • then a plain file would do – Sarthak Mittal Nov 03 '16 at 10:05
  • Could you be more specific ? :) – ArteFact Nov 03 '16 at 10:06
  • @Alexander the `.db` extension is not mandatory. You can have no extension at all. Or your file could have a different extension (i.e.: your app name). Another common extension is `.sqlite`. But I do prefer having an extension, and `.db` is my favourite because it's both very short and very descriptive. – Phantômaxx Nov 03 '16 at 10:13
  • 2
    @ArteFact, please, use `json` or `xml`, and store it in `res/raw` as `your_file.json`, and then in your code access it using Android's `R` class like `R.raw.you_file`. To get data from it, use this http://stackoverflow.com/a/6349913/1796309 – Aleksandr Nov 03 '16 at 10:14
  • 1
    @Rotwang, thank you very much for your note :-) I absolutely agree with you – Aleksandr Nov 03 '16 at 10:16

1 Answers1

2

If I understand you right you have some prepared data to include in app to use.

If you don't want to parse xml, json or raw file every time you need it in the app you could:

1. Parse the file and put contents into SQLite database on the first run of the app.
Then you can use SQLite to query for data.
You can check if this is first run by checking boolean flag in SharedPreferences upon start of application (in onCreate of your main activity or in Application class)

2. If data is not very big you could parse it once and keep it in memory in your custom data structure.
You can create a singleton to query for this data and parse it in case it is not loaded yet.

mckrpk
  • 145
  • 1
  • 7
  • Thanks for your answer, but what I wanted to know was only if you guys knew a better format than xml or json, whiches were the ones I'm used to use. – ArteFact Nov 03 '16 at 13:45
  • Well, there's no android-specific format for data, you just prepare the data in format that suits you best and use/write a parser for loading it. Json is convenient because it's simple to write data and I think there's already a parser in android without including any external library. – mckrpk Nov 05 '16 at 13:56