SQlite or Shared Preferences - android

In my android app I receive data as json like this:
"_playLists":[{"name":"Playlist 1",
"items":[{"name":"Poza 1","target":"...","url":"...","url_thumb":"..."},
{"name":"Poza 2","target":"http:\/\/audifrance.fr","url":"...","url_thumb":..."}]},
{"name":"Playlist 2",
"items":[{"name":"Poza 3","target":"...","url":null,"url_thumb":null}]}]}
My question is : which is the best way to save data from this response: using Shared Preferences or using a database SQlite? Is it possible using Shared Preferences to make connections between the saved data?

If there is no security concern with data, data is complex type and data is in large amount I think SQLite is the best for it.
And if the data is small, data is premitive type and you don't want to share it with user then SharedPreference is good.
Choice is yours.. Thanks.

both have different purposes .
for small sized primitive data's where you don't want to invoke any queries use shared preferences .
for large sized , complex type storage , use sqLite

Related

Can I use two different data storage in one project?

I want to use sharedpreferences in my login form, but i also want keep XAMPP as my main storage. What is the best data storage when I want to do my app in offline mode?
Yes, you're allowed to mix and match any type of data storage you wish because none of them were designed to be in conflict with each other.
However, it's a good idea to understand what type of data storages is available for you to use, because different types of storages has different benefits and drawbacks.
SharedPreferences is a great way to store a user's preferences, or simple data with a single value. It's great for storing a user's preferences because when creating Settings, it's extremely common to use Preferences which directly reflects the values within your SharedPreferences. However, due to it's design, SharedPreferences isn't a good idea to store large amounts of data or dynamically created data.
For that, it's better to use a database and you're free to use any type of database you wish. But for offline mode, it's best to use the SQLite Database that's offered by Android by default.
However, if you do want to use XAMPP, it's not uncommon to see developers who store their full data in an online database, but cache a few data within a local database like SQlite.
Therefore, there's zero conflicts among data storage options, so mix and match to what benefits your app's design best.
If you want to store data in locally then you can use SQLite Database and yeah XAMPP is whole different thing to store data for this you need to create APIs to communicate with database.
You can also use Sharedpreferences for storing the data but Sharedpreferences will store data with the key-value pair only.
But you can use both for different different purposes for the same app

Storing JSON on android: sqlite vs SharedPreference

For an application which will allow members of my organisation to see data on their mobile device i need to store pre-formatted data on the device so they can see it offline as well. How they get the data is trough a JSON request-response.
The response is formatted as follows (anonymised ofc):
[{
"firstname":"John",
"lastname":"Smith",
"group":"1",
"age":11,
"installed":"Ja",
"medical":"Is aan zijn linker zijde verlamd geweest.",
"notes":"Heimee. \r\nBeschermend opgevoed. \r\nTerug getrokken persoonlijkheid.",
"Insignes":["test", "Test2"],
"Parents":[]
},
{
"firstname":"Emely",
"lastname":"Watson",
"group":"33",
"age":14,
"installed":"Ja",
"medical":"",
"notes":"Test",
"Insignes":["Veilig & Gezond I","CWO II","CWO III","Kampeertechnieken & Pionieren"],
"Parents":[
{
"name":"ouder ouder",
"address":"op | 0000AA Amsterdam",
"phone1":"",
"phone2":"0612345678",
"mail":"example#google.com"
}]
}]
I have read a couple of discussion on how to best store this:
Is it ok to save a JSON array in SharedPreferences?
How to save JSON Array in SharedPreferences?
Android: what is the best way to store JSON data offline for the app in android?
Android - how to add JSON object to sharedPreferences?
What is the advantage of Using SQLite rather than File?
From reading these I have gathered that SharedPreference files are "faster" than sqlite but are prone to corruption. SQLite is a database and since the data comes from one I am inclined to use that at the cost of processing speed.
Now I only need to store and then read the data, it wont be mutated unless there is an update on the "main server" in which case I will probably wipe the local data and repopulate it. In these threads i have read that storing JSON in sharedpreference is easy but difficult to read.
But after reading these (and more) discussions I am no closer to knowing/deciding what the best way to store my json is.
Any advice is greatly appreciated!
You can try the ORMs like Realm or sugarORM and store the json has object in the database. They also provide the Cipher options by which you can encrypt the data too, Which would be more flexible.
http://www.androidauthority.com/use-android-keystore-store-passwords-sensitive-information-623779/
http://www.androidhive.info/2016/05/android-working-with-realm-database-replacing-sqlite-core-data/
If you don't need to use any ORM by third parties then you can directly encrypt the JSON string with the android keystore keys and then can store the encrypted string in the normal sqlite.
This is opinion based answer. Android no I wouldn't store JSON in a DB or preferences. Store the json in a file. A file can be accessed as stream the same way you stream access json. Virtually no upper limit on size in a file. I might store a link to the json file in a DB or preferences. Depending on application I might just extract json into abstract elements and store in DB for ordering selection.
The data you have provided depending on sensitivity I would extract and insert into the contacts database for the device.
The best option is to go with a database approach, for one it sounds your dataset might be large are rather expected to be large. Database persist large sets of information, however I do not recommend using SQLITE , for it is sql based, no ORM and the operations can get tedious and time consuming and mainly it is slow(debatable). Rather use the hottest and latest no-sql database for android and ios, which Realm, realm is super fast, it is based on java annotations and ORM(like hibernate). To work with json in android I recommend familiarizing yourself with GSON, A Java serialization/deserialization library that can convert Java Objects into JSON and back.
Shared Preferenences are really convenient to store small key value pairs, If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs. A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. You would of to still work with gson here, I never heard of a shared preference getting corrupted, what I do know is that they are fragile,and live with the existence of your app installation.
Conclusion: If your dataset is large use a database approach to persist large dataset, maintenance and modifications are more fluent here, however if you know that whatever you are storing is relatively small, use a shared pref, and to manipulate json constructs in android to and fro use the google gson library.
SharedPreferences :
If you have small amount of data in Json then you have to store it in SharedPreferences.
You can easily store Json to SharedPreferences using Gson. Just using this :
Convert Json to String :
Gson gson = new Gson();
String jsonString = gson.toJson(jsonData);
Convert String to Json :
gson.fromJson(jsonString, TypeToConvert);
Sqlite Database :
If you have large amount of data in Json then just go with Sqlite.

Android Preferred Storage

We are developing an application in android,We have to save some lists and data to be used by application.We have saved it in sqlite and we use that data by using cursor.
Now my question :
What is more efficient to store data in we should continue with sqlite or to store data on strings.xml or we should use shared preferences ??
Sql Lite is for sharing your data as a datasource. It does not make sense
otherwise as you have the overhead of writing queries to serialize your objects.
Shared Preferences is a preferred way to store internal data.
You can't use assets as said by someone else before because its read only.
Well It Depends on your requirements. If your requirements are dynamic then you should try Sqlite for Storing the Datas.
You should use String.xml to store constant strings, which are not going to change easily ( i mean by increment or decrement of Strings ).
In your scenario, I think best way to store is in asset folder. Means store that data in txt file in key-value pair and while displaying in list, your just have to read that and display in the list. So that saving step can be avoided.
Shared Preferences are generally used to store primitive data. I'd suggest you to go with SQLite .

How to store data from my app

Actually i want to know how to store data from my app in the device so that i can review the store data when i run the application again..
means in simple terms i want to say that suppose i have text box where i write some information..now when i click the submit button, this information will be save,so that when i open the application the stored data should be appear in the text box..
In all terms i want to say that i just want to stored data in the way that we are using database for storing data..so please anyone suggest me how that can be done in android.
if possible show with an example
Regards
Anshuman
If you have to store small amount of data, you can use SharedPreferences in Android.
If the data that you have to store is big/complex enough, try using SQLite database.
Still need help?
UPDATE: There's a tutorial that I wrote to demonstrate how to use SQLite database. check it out here. Although it copies existing database into device's memory, but other versions of it, which create database through code can also be devised from it.
A better tutorial is here : http://www.vogella.com/tutorials/AndroidSQLite/article.html
1) If you want to store data in table format then you can use SQLite database in android
2) If you don't want to store data in table format then you can store in SharedPreference
more info about SharedPreference here and here
Android comes with a built in SQLite database that you can use. I advice you to go trough this notepad tutorial. It teaches the basics of using Android SDK including different states of the android application as well as how to use SQLite with Android.
For storing simple key = value pairs, you can use Properties.
For data storage as in a database, you can use sqlite on android.
Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications (and the user) and how much space your data requires.
Your data storage options are the following:
Shared Preferences
Store private primitive data in key-value pairs.
Internal Storage
Store private data on the device memory.
External Storage
Store public data on the shared external storage.
SQLite Databases
Store structured data in a private database.
Network Connection
Store data on the web with your own network server.
Data Storage

Regarding shared preferences in android

iam taking the data from user and storing in list and now i want to store the multiple data items of the list in shared preferences in android is it possible.
SharedPreferences are not the ideal storage for data lists.
Even if you can do it, you would better consider using a database:
http://developer.android.com/guide/topics/data/data-storage.html#db

Categories

Resources