Android Login and Register with SQLite Database Tutorial
Lets’s Start with Android SQLite Database.
Step 1 – Create a new project in Android Studio 3.0 and select empty Activity, i am going to name the project LoginDatabase.
Step 2 – First of all we need a class which manage our SQLite Database and table. Create a new class in your project and call it SQLiteDBHelper. Our class will extends with SQLiteOpenHelper class, this class will implements two primary override methods for handling and managing our database.
@Override
public
void onCreate(SQLiteDatabase sqLiteDatabase) {...}
@Override
public
void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {...}
Creating Schema Database
Schema define how database is organize, it is just like a SQL statements you use to create database and table. Now Open SQLiteDBHelper class and modify its code looks like mine, make sure to extends with SQLiteOpenHelper class
SQLiteDBHelper.java
public
class
SQLiteDBHelper
extends
SQLiteOpenHelper {
private
static
final
String DATABASE_NAME =
"info.db"
;
private
static
final
int DATABASE_VERSION = 1;
public
static
final
String TABLE_NAME =
"profile"
;
public
static
final
String COLUMN_ID =
"userid"
;
public
static
final
String COLUMN_FULLNAME =
"fullname"
;
public
static
final
String COLUMN_EMAIL =
"email"
;
public
static
final
String COLUMN_PASSWORD =
"password"
;
public
static
final
String COLUMN_MOBILE =
"mobile"
;
private
static
final
String CREATE_TABLE_QUERY =
"CREATE TABLE "
+ TABLE_NAME +
" ("
+
COLUMN_ID +
" INTEGER PRIMARY KEY AUTOINCREMENT, "
+
COLUMN_FULLNAME +
" TEXT, "
+
COLUMN_EMAIL +
" TEXT, "
+
COLUMN_PASSWORD +
" TEXT, "
+
COLUMN_MOBILE +
" TEXT "
+
")"
;
//modified constructor
public
SQLiteDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public
void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLE_QUERY);
}
@Override
public
void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL(
"DROP TABLE IF EXISTS "
+ TABLE_NAME);
onCreate(sqLiteDatabase);
}
}
note : onUpgrade() method is used for upgrade sqlite version and also used to drop he exist
table create new one.
CRUD SAMPLE
I am simply defining a crud operation here for understanding how can we create/read/update/delete into sqlite database, then we will implements Create and Readfunctionality in Login System
INSERTING VALUES INTO TABLE
In order to insert data into sqlite database we use ContentValues class. Following code snippet will insert data into database.
INSERTING THE VALUE INTO DB
SQLiteOpenHelper openHelper =
new
SQLiteDBHelper(this);
SQLiteDatabase db = openHelper.getWritableDatabase();
ContentValues values =
new
ContentValues();
values.put(SQLiteDBHelper.COLUMN_FULLNAME,fullName);
values.put(SQLiteDBHelper.COLUMN_EMAIL,email);
values.put(SQLiteDBHelper.COLUMN_PASSWORD,password);
values.put(SQLiteDBHelper.COLUMN_MOBILE,mobile);
long id = db.insert(SQLiteDBHelper.TABLE_NAME,null,values);
RETRIEVING VALUES FROM TABLE
Following code snippet will retrieve data from sqlite database table.
String[] columns = {
SQLiteDBHelper.COLUMN_ID,
SQLiteDBHelper.COLUMN_FULLNAME,
SQLiteDBHelper.COLUMN_EMAIL,
SQLiteDBHelper.COLUMN_PASSWORD,
SQLiteDBHelper.COLUMN_MOBILE
};
Cursor cursor = db.query(SQLiteDBHelper.TABLE_NAME,columns,null,null,null,null,null,null);
if
(cursor.getCount() > 0) {
while
(cursor.moveToNext()) {
int _id = cursor.getInt(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_ID));
String _fname = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_FULLNAME));
String _email = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_EMAIL));
String _pass = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_PASSWORD));
String _mobile = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_MOBILE));
}
}
DELETING RECORDS IN TABLE
if you want to use delete query using SQLite Database, you can follow the steps below. It is a method which delete row from table using he userid provided in SelectionArgs variable.
public
void DeleteProfile(int userid) {
SQLiteOpenHelper dbhelper =
new
SQLiteDBHelper(this);
SQLiteDatabase db = dbhelper.getWritableDatabase();
// Define 'where' part of query. I am deleteing using userid.
String selection = SQLiteDBHelper.COLUMN_ID +
" = ?"
;
// Specify arguments in placeholder order.
String[] selectionArgs = { String.valueOf(userid) };
// Issue SQL statement.
db.
delete
(SQLiteDBHelper.TABLE_NAME, selection, selectionArgs);
}
UPDATING/MODIFYING RECORDS INTO DATABASE
Updating a record in database is very easy, just specify the columns you want to update in ContentValues class and set the update criteria using selection arguments. In the following snippet I am creating a method which updates fullname and password against the userid.
public
int UpdateProfile(int userid, String fullname, String password) {
SQLiteOpenHelper dbhelper =
new
SQLiteDBHelper(this);
SQLiteDatabase db = dbhelper.getReadableDatabase();
// New value for Two column
ContentValues values =
new
ContentValues();
values.put(SQLiteDBHelper.COLUMN_FULLNAME, fullname);
values.put(SQLiteDBHelper.COLUMN_PASSWORD,password);
// Which row to update, based on the userid column
String selection = SQLiteDBHelper.COLUMN_ID+
" = ?"
;
String[] selectionArgs = { String.valueOf(userid) };
int
count
= db.update(
SQLiteDBHelper.TABLE_NAME,
values,
selection,
selectionArgs);
//Return how many rows updated
return
count
;
}
Let’s take the above idea into Login System.
In Android SQLite Database project i used three activity.
- Login Account Activity [MainActivity]
- RegisterAccountActivity
- LoginSuccessActivity
Creating Login Activity.
Step 3 – Open activity_main.xml and either paste the following xml code or create design yourself, I am using modify version of my own design i discuss in Android Login Layout – Beautifully Designpost. You can follow this post for creating Login Layou Design looks like mine.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".activities.LoginActivity">
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dp"
android:src="@drawable/logo" />
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_email"
android:inputType="text"
android:maxLines="1"
android:textColor="@color/colorText"
android:textColorHint="@color/colorText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_password"
android:textColorHint="@color/colorText"
android:inputType="textPassword"
android:maxLines="1"
android:textColor="@color/colorText" />
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
android:id="@+id/appCompatButtonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:textColor="@color/colorText"
android:background="@color/colorTextHint"
android:text="@string/text_login" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/textViewLinkRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="@string/text_not_member"
android:textSize="16sp"
android:textColor="@color/colorText" />
</android.support.v7.widget.LinearLayoutCompat>
https://github.com/Sainathhiwale/Quiz </android.support.v4.widget.NestedScrollView>
Comments
Post a Comment