what is the purpose of shared preferences and sql lite - android

I am a beginner to android eclipse. I am trying to do one simple application which is to register information in online and receive the same information while searching on the same application who interested to see that information
kindly guide me for this.

Shared Preferences :
You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
Sqlite
SQLite is an Open Source database. SQLite supports standard relational database features like SQL syntax, transactions and prepared statements. The database requires limited memory at runtime (approx. 250 KByte) which makes it a good candidate from being embedded into other runtimes.
SQLite supports the data types TEXT (similar to String in Java), INTEGER (similar to long in Java) and REAL (similar to double in Java). All other types must be converted into one of these fields before getting saved in the database. SQLite itself does not validate if the types written to the columns are actually of the defined type, e.g. you can write an integer into a string column and vice versa.

SQLite will allow you to store persistent data in a very structured way. It's usually used to store data models that your application needs to work with. For example, you make an application that is intended to show car details locally. Those cars should be stored in a SQLite database.
SharedPreferences allow you to store vars that are needed for your application and that should be saved even when the application is closed. For example, Shared Preferences will allow you to save mainly all the preferences of the user (should the application use sound, should it vibrate....).

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

Should I use Android SharedPreferences or SQLite for persistence in my app?

When a user logs in to an app I'm developing, the back-end sends back information about the user. I want to persist the information so it can be used in other activities and when there is no network access.
The two main persistence options I'm aware of for Android are SharedPrefereces and SQLite, and I'm trying to determine which would be more appropriate for my use case. There are three requirements:
The data to store are mostly a small number of primitive data types or simple reference types, but an array or an object of a derived type may also need to be stored.
Reading and writing the data should be as simple as possible.
Data that is written must be available on any subsequent read.
Here is my attempt to compare SharedPreferences and SQLite along these dimensions. (Note that there are other questions on StackOverflow that ask for comparisons of SharedPreferences and SQLite -- "Pros and Cons of SQLite and Shared Preferences", "Saving data on Android : File Storage vs SQLite Database vs Shared Preferences" -- but the answers do not address all three dimensions.)
SharedPreferences
Works most naturally with primitive and simple reference types, but GSON can be used to store data of any type.
Data can be written and read from the UI thread.
Due to a race condition, there is no guarantee that data written to SharedPreferences will be available on any subsequent read.
SQLite
Can be used in a natural way to store structured data of any degree of complexity.
Data should be written and read in a thread separate from the UI thread.
Using a singleton DatabaseHelper guarantees that data written to the database will be available on any subsequent read.
If my assessment is correct, it looks like the SharedPreferences race condition means I have to use SQLite instead to satisfy the requirements even though doing so is more involved. But is my assessment correct?
You said :
a small number of primitive data types or simple reference types
Then definitely SharedPreferences is preferred. If you can image to have a lot of similar rows of data which are all unique rows then use SQLite. You have three requirements and none of them need to store big amount of similar, nevertheless unique, data.
On the other hand:
an array or an object of a derived type may also need to be stored.
i would suggest to store these data in private text files and with every app launch try to decode the data (if it exists).

Best way to save small amout of data on android

I have an android project in which I need to save small amount of data. Either 3 tables with 1-20 entries each or 2 dictionaries with nested dictionaries inside (so not only primary data types) or something close to that. These are simply user preferences and login(s) for the server.
I already read about the options for storing data but I don't know which one is the best for my case. I would like to use shared preferences but I am not quite sure that I can store my nested information there. Does it work? Is there a better solution?
My data (example):
Server logins: Some keys, including data key. Data contains URL, user, password etc.
Favorites: Different info for the items, including server key/data.
The data will be updated rarely, so I can also store some info redundantly to avoid connecting tables or reading from multiple files. The data has to be persistent across sessions and securely stored, not available to any other applications...
I would recommand to use the sqlite Database. This storage is persitent across killed processes and is only private accessable from your App. Have a look here for a first introduction.
SharedPreferences are desigend to store primitive data types, so you cannot easily store nested dictionaries.
Cite from the docs:
The SharedPreferences class provides a general framework that allows
you to save and retrieve persistent key-value pairs of primitive data
types. You can use SharedPreferences to save any primitive data:
booleans, floats, ints, longs, and strings. This data will persist
across user sessions (even if your application is killed).

Storing simple data in android app

I'm making a simple GPA android app. The user can input their grades and class names for each semester. How would I then store each of these semesters so that they can always be pulled up in the app? I might also need to store random variables that are alone.
I've briefly looked at options such as Shared Preferences, Internal Storage, and others. What option is the best for my needs? Please explain why. Thanks!
Here is Explanation...
Shared preferences are good for storing ... an application's preferences, and other small bits of data. It's a just really simple persistent string key store for a few data types: boolean, float, int, long and string. So for instance if my app had a login, I might consider storing the session key as string within SharedPreferences.
Internal storage is good for storing application data that the user doesn't need access to, because the user cannot easily access internal storage. Possibly good for caching, logs, other things. Anything that only the app intends to Create Read Update or Delete.
External storage. Great for the opposite of what I just said. The dropbox app probably uses external storage to store the user's dropbox folder, so that the user has easy access to these files outside the dropbox application, for instance, using the file manager.
SQLite databases are great whenever you a lot of structured data and a relatively rigid schema for managing it. Put in layman's terms, SQLite is like MySQL or PostgreSQL except instead of the database acting as a server daemon which then takes queries from the CGI scripts like php, it is simply stored in a .db file, and accessed and queried through a simple library within the application. While SQLite cannot scale nearly as big as the dedicated databases, it is very quick and convenient for smaller applications, like Android apps. I would use an SQLite db if I were making an app for aggregating and downloading recipes, since that kind of data is relatively structured and a database would allow for it to scale well. Databases are nice because writing all of your data to a file, then parsing it back in your own proprietary format it no fun. Then again, storing data in XML or JSON wouldn't be so bad.
Network connection refers to storing data on the cloud. HTTP or FTP file and content transfers through the java.net.* packages makes this happen.
Considering this i suggest you to use Sqlite especially in your case.
Best luck
it depends on your need, some times you use all options in the same app,
for example : the best way to store grades and classes is using database, in android SqlLite database.
and for storing some variables values like username and password you just need to use shared preferences.... at least this is my policy in my apps.
SQLite will be the best for your scenario.
As you can create well formatted Tables with desired columns. Either you can use pre-developed database or you can create tables on the go.

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

Categories

Resources