Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pros and Cons of SQLite and Shared Preferences [closed]

What is the good mechanism to store information among SQLite database and Shared Preferences?

Why use shared preferences? Why use sqlite? I tried to find the difference between them, and which is the better mechanism for data storing, but I am unable to find the appropriate answer on Google. Please help me with example and explanations.

like image 854
Rana.S Avatar asked Oct 18 '22 19:10

Rana.S


People also ask

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 are the pros and cons of SQLite?

One of the pros in our SQLite pros and cons list is that it's serverless, boosting speed and lowering complexity. However, it also means that the database is confined to the machine it's stored on only. It doesn't allow for remote work on another PC, for instance.

Why you should not use SQLite?

High write volumes: SQLite allows only one write operation to take place at any given time, which significantly limits its throughput. If your application requires lots of write operations or multiple concurrent writers, SQLite may not be adequate for your needs.

What are the advantages of using SQLite?

It reduces application cost because content can be accessed and updated using concise SQL queries instead of lengthy and error-prone procedural queries. SQLite can be easily extended in in future releases just by adding new tables and/or columns. It also preserve the backwards compatibility.


2 Answers

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.

like image 183
Flo Avatar answered Oct 20 '22 07:10

Flo


This question has an accepted answer, but I think there is more to said on the topic - regarding speed.

An application's SharedPreferences and Sqlite DB are both just files, stored in the application's directories on the device's file system. If the amount of data is not too big, the Sqlite option will involve a larger and more complicated file with more processing overhead for simple access.

So, if the nature of the data does not dictate your choice (as explained in accepted answer), and speed matters, then you are probably better to use SharedPreferences.

And reading some data is often on the critical path to displaying the main activty so I think speed is often very important.

One final thought regarding speed and efficiency - if you need to use an Sqlite database for some structured data then it is probably more efficient to also store user preferences in the database so you are not opening a second file. This is a fairly minor consideration - probably worth consideration only if you need to access both the structured data and preferences before you can display the main activity.

like image 112
Tom Avatar answered Oct 20 '22 08:10

Tom