Introduction
In every Android application, we need to save some details on the Android device. For this, there are some options that the Android Operating system provides and those are SQL, File, and SharedPreference. Here, I am going to describe how you can store and retrieve data from Databases in Android. For this, I am using two Activities, one activity contains the Login Page where you can check whether user data exist in the database or not. In RegisterActivity, the user can store his/her details in the database.
For the first one, create the login Activity layout,
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:text="Login Here"
- android:id="@+id/textView"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true" />
- <LinearLayout android:layout_width="match_parent"
- android:layout_below="@+id/textView"
- android:id="@+id/userNameLayout"
- android:layout_margin="10dp"
- android:orientation="horizontal"
- android:layout_height="50dp">
- <TextView
- android:layout_width="0dp"
- android:layout_weight="1"
- android:text="User Name :"
- android:layout_height="wrap_content" />
- <EditText
- android:layout_width="0dp"
- android:layout_weight="2"
- android:id="@+id/username"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <LinearLayout android:layout_width="match_parent"
- android:layout_below="@+id/userNameLayout"
- android:layout_margin="10dp"
- android:orientation="horizontal"
- android:layout_height="50dp"
- android:id="@+id/linearLayout">
- <TextView
- android:layout_width="0dp"
- android:layout_weight="1"
- android:text="Password :"
- android:layout_height="wrap_content" />
- <EditText
- android:layout_width="0dp"
- android:id="@+id/password"
- android:inputType="textPassword"
- android:layout_weight="2"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Login"
- android:id="@+id/loginButton"
- android:layout_below="@+id/linearLayout"
- android:layout_centerHorizontal="true" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Register User"
- android:id="@+id/registerButton"
- android:layout_below="@+id/loginButton"
- android:layout_centerHorizontal="true" />
- </RelativeLayout>
Now create the Registration layout also,
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:text="Register Here"
- android:id="@+id/textView2"
- android:layout_gravity="center_horizontal" />
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_margin="20dp"
- android:layout_gravity="center_horizontal">
- <TextView
- android:layout_width="0dp"
- android:layout_weight="1"
- android:text="Name :"
- android:layout_height="wrap_content" />
- <EditText
- android:layout_width="0dp"
- android:id="@+id/name"
- android:layout_weight="2"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_margin="20dp"
- android:layout_gravity="center_horizontal">
- <TextView
- android:layout_width="0dp"
- android:layout_weight="1"
- android:text="User Name :"
- android:layout_height="wrap_content" />
- <EditText
- android:layout_width="0dp"
- android:id="@+id/userName"
- android:layout_weight="2"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_margin="20dp"
- android:layout_gravity="center_horizontal">
- <TextView
- android:layout_width="0dp"
- android:layout_weight="1"
- android:text="Password :"
- android:layout_height="wrap_content" />
- <EditText
- android:layout_width="0dp"
- android:id="@+id/Password"
- android:inputType="textPassword"
- android:layout_weight="2"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Register"
- android:id="@+id/registerButton"
- android:layout_gravity="center_horizontal" />
- </LinearLayout>
Now comes the main part that is the Database helper class. In Android, there is a class named SQLiteOpenHelper which is an abstract class and is used for the database operations. Firstly, create a class that extends from SQLiteOpenHelper and implement the onCreate() and onUpgrade().
- onCreate() - It is used for creating the database.
- onUpgrade() - It is used for upgrading the database if there is a new version available for the database. Please see the class in detail,
- public class DBHelper extends SQLiteOpenHelper
- {
- private static final int VERSION = 1;
- private static final String DATA_BASE_NAME = "users.db";
- private static final String TABLE_NAME = "login";
- private static final String COLUMN_ID = "id";
- private static final String COLUMN_NAME = "name";
- private static final String COLUMN_USER_NAME = "username";
- private static final String COLUMN_PASSWORD = "password";
- private static final String CREATE_TABLE = "create table " + TABLE_NAME + " (" + COLUMN_ID + " integer primary key not null, " + COLUMN_NAME + " text not null," + COLUMN_USER_NAME + " text not null, " + COLUMN_PASSWORD + " text not null);";
- private SQLiteDatabase db = null;
- public DBHelper(Context context) {
- super(context, DATA_BASE_NAME, null, VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase sqLiteDatabase) {
- sqLiteDatabase.execSQL(CREATE_TABLE);
- db = sqLiteDatabase;
- }
- public void registerUser(UserDetails userDetails) {
- db = this.getWritableDatabase();
- ContentValues contentValues = new ContentValues();
- contentValues.put(COLUMN_NAME, userDetails.getName());
- contentValues.put(COLUMN_PASSWORD, userDetails.getPassword());
- contentValues.put(COLUMN_USER_NAME, userDetails.getUserName());
- db.insert(TABLE_NAME, null, contentValues);
- db.close();
- }
- public boolean isUserExists(UserDetails userDetails) {
- db = this.getReadableDatabase();
- String querry = "select * from " + TABLE_NAME + " where " + COLUMN_USER_NAME + "='" + userDetails.getUserName() + "' and " + COLUMN_PASSWORD + "='" + userDetails.getPassword() + "'";
- Cursor cursor = db.rawQuery(querry, null);
- if (cursor.moveToFirst()) {
- return true;
- }
- return false;
- }
- @Override
- public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {}
- }
- public class UserDetails
- {
- private String userName = null;
- private String password = null;
- private String name = null;
- public String getUserName()
- {
- return userName;
- }
- public void setUserName(String userName)
- {
- this.userName = userName;
- }
- public String getPassword()
- {
- return password;
- }
- public void setPassword(String password)
- {
- this.password = password;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- if (!TextUtils.isEmpty(userName.getText().toString()) && !TextUtils.isEmpty(password.getText().toString())) {
- UserDetails userDetails = new UserDetails();
- userDetails.setPassword(password.getText().toString());
- userDetails.setUserName(userName.getText().toString());
- DBHelper dbHelper = new DBHelper(MainActivity.this);
- if(dbHelper.isUserExists(userDetails))
- Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
- else
- Toast.makeText(MainActivity.this, "Invalid login", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(MainActivity.this, "please enter all the fields", Toast.LENGTH_SHORT).show();
- }
- if (!TextUtils.isEmpty(userName.getText().toString()) && !TextUtils.isEmpty(password.getText().toString()) && !TextUtils.isEmpty(name.getText().toString())) {
- UserDetails userDetails = new UserDetails();
- userDetails.setName(name.getText().toString());
- userDetails.setPassword(password.getText().toString());
- userDetails.setUserName(userName.getText().toString());
- DBHelper dbHelper = new DBHelper(RegisterActivity.this);
- dbHelper.registerUser(userDetails);
- Toast.makeText(RegisterActivity.this, "Successfully registered", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(RegisterActivity.this, "please enter all the fields", Toast.LENGTH_SHORT).show();
- }
Here are the screenshots

Read more articles on Android

Pramod ThakurPosted Apr 17, 2016, 12:07 AM
Nice one..
Asfend YarPosted Apr 16, 2016, 3:17 PM
Will you please try to register the user and share that snapshot ... ???
Kumaresh RajalingamPosted Apr 16, 2016, 8:36 AM
Good one
Debasis SahaPosted Apr 16, 2016, 2:39 AM
Nice One..
Vignesh ManiPosted Apr 15, 2016, 4:26 PM
Nice one
Kuppurasu NagarajPosted Apr 15, 2016, 1:18 PM
Nice Sharing..
Rahul Kumar SaxenaPosted Apr 15, 2016, 7:13 AM
Good Show