
What is Sqlite Database in Android ?
Sqlite Database is an open source relational Database.It is used to perform database operations on android devices such as storing, manipulating or retrieving persistent data from the database.
It is embedded in android by default. No need setup any database.
SQLite supports all the relational database features. Android is Built in Database Implementation.
No need to any connections of like JDBC,ODBC e.t.c.
SqliteOpenHelper class provides the SQLite database functionality.
SqliteOpenHelper Constructor
There are two type of Constructor.
SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
In this constructor used to creates an object for creating, opening and managing the database.
SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler)
In this Constructor used to creates an object for creating, opening and managing the database. It specifies the error handler.
Methods of SqliteOpenHelper Class
public abstract void onCreate(SQLiteDatabase db) : In this Method used to database is created for the first time.
public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) : In this Method is used to database needs to be upgraded.
public synchronized void close () : In this Method is used to closes the database object.
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) : In this Method is used to database needs to be downgraded.
Methods of SQLiteDatabase
void execSQL(String sql)
In this method used to executes the sql query it is not select query.
long insert(String table, String nullColumnHack, ContentValues values)
In this Method used to Inserts record on the database And Table specifies the table name, nullColumnHack doesn’t allow completely null values. If second argument is null, android will store null values if values are empty. The third argument specifies the values to be stored.
int update(String table, ContentValues values, String whereClause, String[] whereArgs) : In this Method used to Update row in the Database table.
Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) : In this Method used to Retrieve Data from Database and it can return cursor. retrieve anything from database using an object of the Cursor class and it will return a resultset with the cursor pointing to the table.
Example Of SQLite Database In Android
Create activity_main.xml File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/colorAccent" tools:context="com.example.bhaumik.sqldemo.MainActivity"> <Button android:id="@+id/btn_add_contact" style="@style/ButtonStyle" android:text="Add Contacts" /> <Button android:id="@+id/btn_update_contact" style="@style/ButtonStyle" android:text="Update Contacts" /> <Button android:id="@+id/btn_delete_contact" style="@style/ButtonStyle" android:text="Delete Contacts" /> <Button android:id="@+id/btn_search_contact" style="@style/ButtonStyle" android:text="Search Contacts" /> </LinearLayout> |
Create MainActivity.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
package com.example.bhaumik.sqldemo; import android.content.Intent; import android.provider.ContactsContract; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.crashlytics.android.Crashlytics; import io.fabric.sdk.android.Fabric; public class MainActivity extends AppCompatActivity{ Button add,update,delete,search; DatabaseHelper databaseHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Fabric.with(this, new Crashlytics()); setContentView(R.layout.activity_main); databaseHelper = new DatabaseHelper(this); init(); setListener(); } private void init() { add = (Button) findViewById(R.id.btn_add_contact); update = (Button) findViewById(R.id.btn_update_contact); delete = (Button) findViewById(R.id.btn_delete_contact); search = (Button) findViewById(R.id.btn_search_contact); } private void setListener() { add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,AddContactActivity.class); startActivity(intent); } }); update.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,UpdateContactActivity.class); startActivity(intent); } }); delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,DeleteContactActivity.class); startActivity(intent); } }); search.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,SearchContactActivity.class); startActivity(intent ); } }); } } |
Create activity_add_contact.xml File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/colorPrimary" tools:context="com.example.bhaumik.sqldemo.AddContactActivity"> <EditText android:id="@+id/et_name" style="@style/EditTextStyle" android:textColorHint="@color/WhiteColor" android:hint="Enter Name"/> <EditText android:id="@+id/et_email" style="@style/EditTextStyle" android:textColorHint="@color/WhiteColor" android:hint="Enter Email"/> <EditText android:id="@+id/et_mobile" style="@style/EditTextStyle" android:hint="Enter Mobile No" android:inputType="phone" android:textColorHint="@color/WhiteColor" android:imeOptions="actionDone"/> <Button android:id="@+id/btn_add" android:layout_width="250dp" android:layout_height="50dp" android:text="Add " android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:background="@color/ButtonColor" android:textColor="@color/WhiteColor" android:textSize="18dp" /> </LinearLayout> |
Create AddContactActivity.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package com.example.bhaumik.sqldemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class AddContactActivity extends AppCompatActivity { EditText name,email,mobile; Button add; DatabaseHelper databaseHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_contact); databaseHelper = new DatabaseHelper(this); init(); setListener(); } private void setListener() { add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean result = databaseHelper.InsertData(name.getText().toString(),email.getText().toString(),mobile.getText().toString()); if(result) { Toast.makeText(AddContactActivity.this,"Contact Inserted Successfully...",Toast.LENGTH_SHORT).show(); name.setText(""); email.setText(""); mobile.setText(""); } else { Toast.makeText(AddContactActivity.this,"Failed Contact Inserted...",Toast.LENGTH_SHORT).show(); } } }); } private void init() { name = (EditText) findViewById(R.id.et_name); email = (EditText) findViewById(R.id.et_email); mobile = (EditText) findViewById(R.id.et_mobile); add = (Button) findViewById(R.id.btn_add); } } |
Create activity_update_contact.xml File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.bhaumik.sqldemo.UpdateContactActivity"> <EditText style="@style/EditTextStyle" android:id="@+id/et_update_mobile" android:hint="Enter Mobile No" android:inputType="phone" android:textColor="@color/ButtonColor"/> <Button style="@style/ButtonStyle" android:id="@+id/btn_search_updates" android:text="Search"/> <EditText style="@style/EditTextStyle" android:id="@+id/et_update_name" android:hint="Your Name" android:textColor="@color/ButtonColor"/> <EditText style="@style/EditTextStyle" android:id="@+id/et_update_email" android:hint="Your Email" android:textColor="@color/ButtonColor"/> <Button style="@style/ButtonStyle" android:id="@+id/btn_update" android:text="Update"/> </LinearLayout> |
Create UpdateContactActivity.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
package com.example.bhaumik.sqldemo; import android.database.Cursor; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class UpdateContactActivity extends AppCompatActivity { EditText mobile,name,email; Button search,update; DatabaseHelper databaseHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_update_contact); databaseHelper = new DatabaseHelper(this); init(); setListener(); } private void setListener() { search.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Cursor cursor = databaseHelper.SearchData(mobile.getText().toString()); while (cursor.moveToNext()) { name.setText(cursor.getString(1)); email.setText(cursor.getString(2)); } } }); update.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { databaseHelper.UpdateData(mobile.getText().toString(),name.getText().toString(),email.getText().toString()); Toast.makeText(UpdateContactActivity.this,"Contact Updated Successfully..",Toast.LENGTH_SHORT).show(); mobile.setText(""); name.setText(""); email.setText(""); } }); } private void init() { mobile = (EditText) findViewById(R.id.et_update_mobile); name = (EditText) findViewById(R.id.et_update_name); email = (EditText) findViewById(R.id.et_update_email); search = (Button) findViewById(R.id.btn_search_updates); update = (Button) findViewById(R.id.btn_update); } } |
Create activity_delete_contact.xml File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.bhaumik.sqldemo.DeleteContactActivity"> <EditText style="@style/EditTextStyle" android:id="@+id/et_delete_mobile" android:textColor="@color/ButtonColor"/> <Button style="@style/ButtonStyle" android:id="@+id/btn_delete" android:text="Delete"/> </LinearLayout> |
Create DeleteContactActivity.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
package com.example.bhaumik.sqldemo; import android.database.Cursor; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class UpdateContactActivity extends AppCompatActivity { EditText mobile,name,email; Button search,update; DatabaseHelper databaseHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_update_contact); databaseHelper = new DatabaseHelper(this); init(); setListener(); } private void setListener() { search.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Cursor cursor = databaseHelper.SearchData(mobile.getText().toString()); while (cursor.moveToNext()) { name.setText(cursor.getString(1)); email.setText(cursor.getString(2)); } } }); update.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { databaseHelper.UpdateData(mobile.getText().toString(),name.getText().toString(),email.getText().toString()); Toast.makeText(UpdateContactActivity.this,"Contact Updated Successfully..",Toast.LENGTH_SHORT).show(); mobile.setText(""); name.setText(""); email.setText(""); } }); } private void init() { mobile = (EditText) findViewById(R.id.et_update_mobile); name = (EditText) findViewById(R.id.et_update_name); email = (EditText) findViewById(R.id.et_update_email); search = (Button) findViewById(R.id.btn_search_updates); update = (Button) findViewById(R.id.btn_update); } } |
Create activity_search_contact.xml File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.bhaumik.sqldemo.SearchContactActivity"> <EditText style="@style/EditTextStyle" android:id="@+id/et_search_mobile" android:hint="Enter Mobile No" android:textColor="@color/ButtonColor" android:inputType="phone" /> <Button style="@style/ButtonStyle" android:id="@+id/btn_search" android:text="Search"/> <TextView style="@style/TextViewStyle" android:id="@+id/tv_search_name" /> <TextView style="@style/TextViewStyle" android:id="@+id/tv_search_email"/> </LinearLayout> |
Create SearchContactActivity.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package com.example.bhaumik.sqldemo; import android.database.Cursor; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class SearchContactActivity extends AppCompatActivity { EditText mobile; TextView name,email; Button search; DatabaseHelper databaseHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search_contact); databaseHelper = new DatabaseHelper(this); init(); setListener(); } private void setListener() { search.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Cursor cursor = databaseHelper.SearchData(mobile.getText().toString()); while (cursor.moveToNext()) { name.setText(cursor.getString(1)); email.setText(cursor.getString(2)); } } }); } private void init() { mobile = (EditText) findViewById(R.id.et_search_mobile); name = (TextView) findViewById(R.id.tv_search_name); email = (TextView) findViewById(R.id.tv_search_email); search = (Button) findViewById(R.id.btn_search); } } |
In this Example of I have Create Insert,Delete,Update and Search Contact from SQLite Database Stored. it can different different activity to perform database related operation. first of all create Database Helper File and then implements SQLite Database and then override methods implement and i have create database name,database version.
In this Database file to create some methods to insert data delete data search data from database. and then called this method to different different activity and then perform activity task.
It is Simple Example of SQLite Database to stored contact form detail on the database table.and then delete search and update this information to access on database.