Working With Shared Preferences and SQlite Android

Posted by Deepak Kumar
October 27, 2017

Self-Published

Why we need databases ?

Everytime you interact with the application, and changes something in that. you want to save that data so that whenever you open the app next time, the changes you made should be there. And, it is only possible by saving those data.

In Android we use SQLite database to save data. It stores data in form of table.While for saving key-value pairs we use Shared Preferences.

  • #1 How to Implement SQlite Database :Define Schema of the database.Schema is basically the blueprint that tells you how your data is organized in database. The schema is reflected in the SQL statements that you use to create your database. It may be also helpful if you create a contract class. Contract class is a container that store the constants that define the name of table an columns.
  • Create a DatabaseHandler class and extend to SQLiteOpenHelper class, this class will be used to open SQlite connection and create and modify the database. Now in this you have to override two methods ( as it is a abstract class ), onCreate and onUpgrade methods.
public class DatabaseHandler extends SQLiteOpenHelper {
    // If you change the database schema, you must increment the database version.
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "Student.db";
    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public void onCreate(SQLiteDatabase db) {
      //statements
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over

}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}

What are Shared Preferences and how to implement ?

Shared preferences is one of the method you can save data in android, it simply allows you to store and retrieve the data in key-value pair. If you didn’t understand about key-value pair. Here is an example :

"key" : "value"
"username" : "dipakkr"
"password" : "12345678"

So, here as you can see the above pairs, username and password are keys, while their corresponding data are values.

Implementation :

For Saving data in Shared Preferences :

SharedPreferences pref =PreferenceManager.getSharedPreferences(this); Editor editor = pref.edit(); editor.putString("username","dipakkr"); editor.putString("password",12345678"); editor.commit();

For Retrieving Data from Shared Preferences :

SharedPreferences pref =PreferenceManager.getSharedPreferences(this); String username = getString("username",""); //print username

So, This how the SharedPreferences in Android works.

For More tutorials and codes of the application visit My GitHub Repository.

I will be keep updating this Blog and GitHub Repository. Happy Coding & Reading !!

Cheers!!

Youth Ki Awaaz is an open platform where anybody can publish. This post does not necessarily represent the platform's views and opinions.