How would you store and access data in Android app? - android

I am making a dictionary app and need to store and access my data from the app. My goal is to make a completely offline dictionary.
I have all the data needed in JSON format (the file is about 9 MB), so I can convert it into any other format or even into sqlite database.
I try to dig some guides, but it appears to me that Android apps use sqlite only for user data. But I know that there are plenty of other offline dictionaries.
How do they store words and translations internally in the app? And what is the best way to do that?

For the amount of data you need to store, SqlLite is the best option. Sqlite has a wide array of applications in Android apps; their scope is not limited to storing user data. The next best option would be to save the Json in a text file. You can then load the Json data in memory (when the app starts) and transform that to a dictionary structure with the JSON parsers.
Downside of JSON: You need to load your entire stored JSON in memory before parsing it. With 9MB, its not a good idea.
SQLite is the best option if you do not want your 'entire' dictionary in memory at all times and need values only on a look up basis. You also get the full functionality of basic SQL statements with SQLite. Also, there is a lot of SQLite help online; choosing tried and tested technology is always good.

Take a look at the developers website: http://developer.android.com/guide/topics/data/data-storage.html

Related

Caching in Android

I'm currently developing an android app in which I'm dealing with php files that returns json, the json then gets parsed and is used to fill up an arraylist of different objects.
What I'm trying to do is, I want to cache these objects in case of no internet so I can reload them and make the application useable offline.
Currently I have 2 solutions for this:
A) Make an SQLite database and creare tables with the same structure as the objects I'm trying to cache and then reload them on startup
B) Save the JSON strings inside the shared prefs and parse them on startup.
I didn't really find any best practices or tutorials when I came across data caching so I'm lost now and I have no idea what to do. So if you guys can please help out I'd be thankful.
You might find the following talk interesting:
https://www.youtube.com/watch?v=xHXn3Kg2IQE
As you can see, a lot of scaffolding goes into the ContentProvider approach. For a very simple app, it can be acceptable to just cache some JSON in a SharedPreferences. However, as your app becomes more complex, the advantages of the ContentProvider will be worth it.
You can use files instead of sqlite and shared preferences.
Shared prefrences is using for small data.
Sqlite is using when you need to make queries on stored data.
So I think it's better to cache in files, or if you are using something like retrofit or any network lib you can check if they support caching and use it.
Definitely not option B. Shares preferences are not suitable for storing large amounts of data. It's XML so cannot be queried like Sqlite. The time to retrieve a single item will increase linearly with the amount of data stored. Secondly JSON will have special character these will need to be escaped which means the storage size will increase even more.
There is however an option C. Using cache files. This approach and other store options available to you are described in the google developer guide and this is essential reading.
So in summary: you options are to parse the JSON and store it in sqlite or to save the json as a file in the cache directory.

Android Persistent Storage

I have read through the Android Storage Options and I have a question that I haven't been able to find the answer to:
Should I use SQLite to store my data or should I use a JSON object that is written to a file?
Requirements:
Store (up to) a few hundred instances of the same object. Each instance will be somewhat complex, storing reference to images, smaller objects, etc. The data will be stored locally, with the option of cloud backup. All the data will be loaded on startup and saved when manipulated by the user.
The reason I ask this is because I don't have a lot of data to store - for a SQLite database there will probably never be more than a few 100 rows, which makes me think SQL is overkill.
Also, exporting my data to a JSON file will allow me to easily import/export from different device platforms (I already do this on iOS).
Or, maybe there's a better option? If there was an NSCoding type library for Android I would probably use that.
Any opinions are helpful.
Thanks!
From the presented so far, storing in files will be more advantageous.
Considering that each "unit" is less than 16 attributes, a json file with short identifiers will likely generate a larger file representation than the SQL representation equivalent.
However, the local file manipulation will allow for easier interactions, as well as easier backing up/down.
Also, the File class is simple enough to generate less issues when compared to SQL.
Finally, given the choices, you are going to have to evaluate the operations used.
If you are going to compare the data, then SQL is likely to go faster, but if you are just inputting/outputting each data as a separate object, than files are going to be as fast as SQL.
Finally, please, particionate your objects, do not create just 1 file with all the info.
I have read through the Android Storage Options and I have a question
that I haven't been able to find the answer to:
Should I use SQLite to store my data or should I use a JSON object
that is written to a file?
You need to analyse your requirement again.
maybe there's a better option?
It depends upon your requirement.
if Your requirement is fixed to simply storing and retrieving then you can have a look on tinnyDB, which is basically using the SharedPreferences as storage mechanism. But if you need case base based selection/query of data then you should go with SQLite.

Data Persistence Android

I'm developing a social photo app that is like instagram and I need to improve the application performance, to do that I need to persist some data, like timeline feed, photos, messages, etc.
I'm already using Shared Preferences to save user informations.
I don't know which is the best way to persist the data, I'm between SQLite or Files, to save the json data or something else.
Which is faster and gives me less trouble?
If somebody could help me I would appreciate it.
take a look at this caching images will provide better solution.
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
If you need different queries on this data, than i strongly recommend to use SQLite. Cause it's not possible to make select queries on files without storing it to memory.
Even if you use this object as is without any special queries, you can store json objects to base.
SQL:
1) Fast.
2) Possibility to make select queries with different WHERE condition.
3) Easy update.
4) etc
I do not see the advantages to store this data to files...
If you will store data in files, it will slow if there will be a lot of data. Even if you will use sax parser.
But, images you should store as files, and only update link on it in data base.

Android data storage, When to use SqlLite and when to use JSON, Linq alternatives

I have a few years of exp with webdev using .Net and C#... and one feature I really enjoy there is the linq-expressions for quering for data.. is there anything that simulair for Android using to query against JSONs or against the SqlLite database? (I guess im looking for a typed expression/query-framework).
And for my next question.. Which one of the JSON approach and the SQLLite approach am I supposed to use.. and when? I know this has been asked a million times.. but there doesnt seem to be any strict answers..
Is SQLLite for more complex querying and when Im having a bigger ammount of data, while JSON is used while having a smaller ammount of data and not very much querying is needed?
Could I store all my JSON data in the memmory for faster read/write access?..since I have heard that storing it in the storage/on disk might be a slow process.
But on the other hand I also have heard that SQLLite is slow in generall...
And finally.. when we are speaking of "slow" is it slow like, a single read/write looks up the entire application for a few milisecs or is it slow like compared to a in-memory-database that executes queries and stuff fast as lightning
When to use SQLite
SQLite is one form of how to persist your data, how to secure your data. Generally if you want to store larger quantum of data and have quick access to them(and also if these data are "sensitive"), SQLite is great choice. Hence i disagree with your opition that SQLite is slow, definitely not i guess.
I have SQLite database with approximately with 650 000 records and still smart performance(an usage of indexes is sometimes necessary).
When to use JSON
JSON is lightweight data structure designed for human-readable data interchange and what is main is language-independent. It's very good choice(maybe the best) if you want send data via network(send data to remote server etc.) but i think not very good for data persisting also it's not safe as SQLite is.
Generally you should compare JSON with XML but you can't compare SQLite and JSON which are two different things.
General Idea
SqlLite : For Local Storage
JSON : For Server SideStore
It doesn't make sense to compare SQLite and JSON. One is a storage solution, the other one is a data format.
You should probably read the official guide before deciding which storage solution to choose (SQLite, preferences, file system,...): http://developer.android.com/guide/topics/data/data-storage.html
If you want to go for SQLite persistence you can use some third-party ORM to make queries easier (e.g ORMLite is the most popular one)

How to storing data in my own application

I would like to build some simple application - for example Todo list - and I am thinking about the problem and its solving - how can I to store data in my own application on Android platform?
I should to use some text file, xml file or some database? What will be better for beginner on this field?
You'll have a VERY hard time getting anywhere with Android if you don't read through their website/dev resources. I would highly recommend visiting their site.
As far as data storage is concerned, that varies based on your need. Explained here
My suggestion is to use SQLite that comes with Android. http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
In my opinion, a SQLite database seems most appropriate for this kind of application. There is lots of support for using the SQLite database in conjuction with ListViews (which I imagine you'd want to use in your to-do list app).
In case you haven't already checked it out, see the page on the developer site:
http://developer.android.com/guide/topics/data/data-storage.html#db
Theres a few ways you can store data. I've created a few applications thats store data using shared preferences
They're quite handy for storing strings, ints, bool values etc. However if you have a large scaled application, that requires better database management, I would look into sqlLite.
Android has supported classes and functions to help access/store the information.
Theres a good tutorial on the android site called notepad that takes you through how to use sqlite.
That should get you started :)
Look at this Thread klick
Edit: its cool for less data, if you want to store and browse lots of data, you should use as SQLite Database
Using a file, database or xml based depends entirely on what kind of application you are building. For eg: If you parse an XML feed and store the results back in an XML file - it totally defeats the storage purpose!
Databases are used to store structured and related content like - news feed results, email client data, etc.
Files are more used for storing raw / binary content like storing images, attachments, etc.
BTW, if you are a beginner - you should try all of them! :)
Hope this helps!

Categories

Resources