Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between shared preference and sqlite

Tags:

android

I know this topic has been discussed before on Stack Overflow. But there are still some things that are not clear when I read previous posts about it. So here they are:

  • I know that we use shared preference for small datasets and sqlite for large data manipulation, so if we just want to save a username and password should we use shared preferences?
  • Won't shared preferences be lost when user uninstalls the app? For example I download an app called abc and save my username and password. Then I uninstall this app from one phone and try to access it from other phone using the same username and password. Will this be saved using shared preferences or the data be lost?
  • What are the main reason we use one over the other beside large and small datasets?
like image 277
NoviceMe Avatar asked Mar 06 '12 19:03

NoviceMe


People also ask

What is difference between shared preferences and SQLite?

Shared preferences can only store key-value pairings whilst an SQLite database is much more flexible. So shared preferences are particularly useful for storing user preferences, e.g. should the app display notifications etc. Whilst an SQLite database is useful for just about anything.

Which one is better shared preferences and SQLite?

For storing huge amount of data, go for SQLite database system. This will allow the user to search for data as well. On the other hand, for storing small amount of data, go for Shared Preferences. In this case, a huge database system is unnecessary.

What is the difference between room and SQLite?

Room is an ORM, Object Relational Mapping library. In other words, Room will map our database objects to Java objects. Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite. In case of SQLite, There is no compile time verification of raw SQLite queries.

What is the use of SharedPreferences?

A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared. This page shows you how to use the SharedPreferences APIs to store and retrieve simple values.


5 Answers

1.SharedPreferences stores only Boolean, int, float, long, String five kinds of simple data types, such as can not be conditional query. So, whether SharedPreferences data storage operation is how simple it can only be a supplement of storage, but can not completely replace other data such as the SQLite database is stored.

2.SharedPreferences based on the XML file to store key-value key used to store configuration information(mainly user preference for your application).

3.Sharedprefrece just like cookies in web which store some basic information at client side.

like image 158
ρяσѕρєя K Avatar answered Oct 30 '22 00:10

ρяσѕρєя K


You can think of the difference between shared preferences and an SQLite database in terms of data size but that isn't entirely accurate. A better way to think of it is in terms of the structure of the data you want to store.

Shared preferences can only store key-value pairings whilst an SQLite database is much more flexible. So shared preferences are particularly useful for storing user preferences, e.g. should the app display notifications etc. Whilst an SQLite database is useful for just about anything.

Both data sources are local but something you should be aware of is the ability to backup your application data to cloud storage that is linked to the user's Google account. This makes it much easier for your users to change devices and for their applications to easily transfer to the new device. For more info take a look here.

like image 25
Charles Harley Avatar answered Oct 30 '22 01:10

Charles Harley


In the situation you described about you will lose the user name and password in both situations. The data is stored on the phone, when you uninstall the application, the data that some with it will also be lost. The user will have to re-enter this information.

You can save the user name and pass in either the shared Preferences or a DB, that is personal preference. Just make sure you lock either down, i.e. don't share the DB or Shared Preferences that you keep this information in.

As for the difference... shared Preferences should hold well... shared Preferences... here is an example:

If I create an option to change the background color, I will store all available options in a DB that can be loaded into a adapter view for the user to choose from. But I will store the color that they have selected in the Shared Preferences. This way when the application load I can get the Shared Preference value of the background color that should be used.

like image 38
jjNford Avatar answered Oct 29 '22 23:10

jjNford


SharedPreferences is used for just that, storing user preferences shared application-wide. You can use it, for example, to store a user's username, or perhaps some options he or she has configured in your app in which you want to remember.

SQLite is a relational database. It's used to store your application's data, not preferences or configuration information.

Both are stored locally on the device.

like image 33
Tyler Treat Avatar answered Oct 30 '22 01:10

Tyler Treat


both store their data locally, so uninstalling the app will delete both. other than that, SharedPreferences is easier to program, and you're right about the data amounts.

like image 28
Bill Gary Avatar answered Oct 30 '22 01:10

Bill Gary