Pros and Cons of Sqlite and Shared Preferences

Pros and Cons of SQLite and Shared Preferences

It really depends on the data you want to store.

SQLite

Large amounts of same structured data should be stored in a SQLite database as databases are designed for this kind of data. As the data is structured and managed by the database, it can be queried to get a sub set of the data which matches certain criteria using a query language like SQL. This makes it possible to search in the data. Of course managing and searching large sets of data influences the performance so reading data from a database can be slower than reading data from SharedPreferences.

SharedPreferences

SharedPreferences is a key/value store where you can save a data under certain key. To read the data from the store you have to know the key of the data. This makes reading the data very easy. But as easy as it is to store a small amount of data as difficult it is to store and read large structured data as you need to define key for every single data, furthermore you cannot really search within the data except you have a certain concept for naming the keys.

Android SharedPreferences or SQLite Storing

Shared Preferences:

All shared prefs are stored in /data/data/[package name]/shared_prefs/[app name].xml, so i think there's no limit based on aechitecture.

Shared Preferences is nothing but a simple table which has two columns. (key, value).

Shared Preference size 8192 characters according to this documentation:
http://developer.android.com/reference/java/util/prefs/Preferences.html#MAX_VALUE_LENGTH

advantages:

  • Fast retrieval
  • Easy to understand and program

disadvantages

  • Maintaining Keys are difficult if we store a lot of values.
  • User can clear this at any time.

Database:

When we have a lot of values to store with complex structure, we are left with only one great solution, ie. DB.

advantages

  • We can maintain structure of data.
  • Android has nice and simple APIs to handle sqlite operations.

disadvantages

Operation is little bit slow comparing to shared preferences.
user can clear this at any time.

Reference

SharedPreferences advantages & disadvantages in small application

When yo use SharedPrefrences and when to use SQLite it really depends upon the data and there is good explanation Here when you have to use sharedprefence and when you have to use SQLite

Difference between storing data in shared preferences and database in android

The main difference between SharedPreferences and DataBase is like you mentioned :

SharedPreferences works on an Key-Value pair basis. you simply provide the Key and get back the Value you stored. that's great.

DataBase creates an SQLite Tables and you need to use queries to pull them out.

I think that if you are good with the JSON mechanism that you built, then storing a string in SharedPreferences is all you need.

But when the Data get more and more complex, and you would like quick access to any part of it, I think DB would be easier than parsing and seaching a JSON string all the time.
Yes, it might make you write more code for handling the DB queries..



Related Topics



Leave a reply



Submit