Android SQLite Database with Examples - Tutlane
Android SQLite Database with Examples - Tutlane
SQLite (/tutorial/sqlite) is an open-source lightweight relational database management system (RDBMS) to perform database
operations, such as storing, updating, retrieving data from the database. To know more about SQLite (/tutorial/sqlite), check this
SQLite Tutorial with Examples (/tutorial/sqlite).
By default, Android comes with built-in SQLite Database (/tutorial/sqlite) support so we don’t need to do any configurations.
Just like we save the files on the device’s internal storage, Android stores our database in a private disk space that’s associated
with our application and the data is secure, because by default this area is not accessible to other applications.
The package android.database.sqlite contains all the required APIs to use an SQLite database in our android applications.
Now we will see how to create a database and required tables in SQLite and perform CRUD (insert, update, delete and select)
operations in android applications.
Following is the code snippet of creating the database and tables using the SQLiteOpenHelper class in our android application.
If you observe above code snippet, we are creating database “usersdb” and table “userdetails” using SQLiteOpenHelper class by
overriding onCreate and onUpgrade methods.
Method Description
onCreate() This method is called only once throughout the application after the database is created and the table creation
statements can be written in this method.
onUpgrade() This method is called whenever there is an updation in the database like modifying the table structure, adding
constraints to the database, etc.
Now we will see how to perform CRUD (create, read, delete and update) operations in android applications.
Following is the code snippet to insert data into the SQLite database using the insert() method in the android application.
https://www.tutlane.com/tutorial/android/android-sqlite-database-with-examples 1/6
2/21/23, 5:17 PM Android SQLite Database with Examples - Tutlane
If you observe above code, we are getting the data repository in write mode and adding required values to columns and inserting
into database.
Following is the code snippet to read the data from the SQLite Database (/tutorial/sqlite) using a query() method in the android
application.
If you observe above code, we are getting the details from required table using query() method based on our requirements.
Following is the code snippet to update the data in the SQLite database using an update() method in the android application.
If you observe above code, we are updating the details using update() method based on our requirements.
Following is the code snippet to delete the data from the SQLite database using the delete() method in the android application.
If you observe above code, we are deleting the details using delete() method based on our requirements.
Now we will see how to create sqlite database and perform CRUD (insert, update, delete, select) operations on SQLite Database
(/tutorial/sqlite) in android application with examples.
Create a new android application using android studio and give names as SQLiteExample. In case if you are not aware of creating
an app in android studio check this article Android Hello World App (/tutorial/android/android-hello-world-app-example).
Once we create an application, create a class file DbHandler.java in \java\com.tutlane.sqliteexample path to implement SQLite
database related activities for that right-click on your application folder à Go to New à select Java Class and give name as
DbHandler.java.
Once we create a new class file DbHandler.java, open it and write the code like as shown below
DbHandler.java
package com.tutlane.sqliteexample;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by tutlane on 06-01-2018.
*/
https://www.tutlane.com/tutorial/android/android-sqlite-database-with-examples 2/6
2/21/23, 5:17 PM Android SQLite Database with Examples - Tutlane
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Users);
// Create tables again
onCreate(db);
}
// **** CRUD (Create, Read, Update, Delete) Operations ***** //
If you observe above code, we implemented all SQLite Database (/tutorial/sqlite) related activities to perform CRUD operations in
android application.
Now open activity_main.xml file from \res\layout folder path and write the code like as shown below.
activity_main.xml
<?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:id="@+id/fstTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="150dp"
android:text="Name" />
<EditText
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10"/>
<TextView
android:id="@+id/secTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location"
(/)
https://www.tutlane.com/tutorial/android/android-sqlite-database-with-examples 3/6
2/21/23, 5:17 PM Android SQLite Database with Examples - Tutlane
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/txtLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<TextView
android:id="@+id/thirdTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Designation"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/txtDesignation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Save" />
</LinearLayout>
Now we will create another layout resource file details.xml in \res\layout path to show the details in custom listview
(/tutorial/android/android-listview-with-examples) from SQLite Database (/tutorial/sqlite) for that right click on your layout
folder à Go to New à select Layout Resource File and give name as details.xml.
Once we create a new layout resource file details.xml, open it and write the code like as shown below
details.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/user_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp" />
<Button
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Back" />
</LinearLayout>
Create an another layout file (list_row.xml) in /res/layout folder to show the data in listview (/tutorial/android/android-listview-
with-examples), for that right click on layout folder à add new Layout resource file à Give name as list_row.xml and write the
code like as shown below.
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="17dp" />
<TextView
android:id="@+id/designation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_marginTop="7dp"
android:textColor="#343434"
android:textSize="14dp" />
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/designation"
android:layout_alignBottom="@+id/designation"
android:layout_alignParentRight="true"
android:textColor="#343434"
android:textSize="14dp" />
</RelativeLayout>
Now open your main activity file MainActivity.java from \java\com.tutlane.sqliteexample path and write the code like as shown
below
(/)
https://www.tutlane.com/tutorial/android/android-sqlite-database-with-examples 4/6
2/21/23, 5:17 PM Android SQLite Database with Examples - Tutlane
MainActivity.java
package com.tutlane.sqliteexample;
import android.content.Intent;
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;
If you observe above code, we are taking entered user details and inserting into SQLite database and redirecting the user to
another activity.
Now we will create another activity file DetailsActivity.java in \java\com.tutlane.sqliteexample path to show the details from
the SQLite database for that right-click on your application folder à Go to New à select Java Class and give name as
DetailsActivity.java.
Once we create a new activity (/tutorial/android/android-activity-lifecycle) file DetailsActivity.java, open it and write the code like
as shown below
DetailsActivity.java
package com.tutlane.sqliteexample;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by tutlane on 05-01-2018.
*/
If you observe above code, we are getting the details from SQLite database and binding the details to android listview
(/tutorial/android/android-listview-with-examples). Now we need to add this newly created activity (/tutorial/android/android-
activity-lifecycle) in AndroidManifest.xml file in like as shown below.
(/)
https://www.tutlane.com/tutorial/android/android-sqlite-database-with-examples 5/6
2/21/23, 5:17 PM Android SQLite Database with Examples - Tutlane
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutlane.sqliteexample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DetailsActivity" android:label="SQLite Example - Details"></a
ctivity>
</application>
</manifest>
If you observe above example, we are saving entered details in SQLite database and redirecting the user to another activity
(/tutorial/android/android-activity-lifecycle) file (DetailsActivity.java) to show the users details and added all the activities in
AndroidManifest.xml file.
If you observe the above result, the entered user details are storing in the SQLite database and redirecting the user to
another activity (/tutorial/android/android-activity-lifecycle) file to show the user details from the SQLite database. After that, if we
click on the Back button, it will redirect the user to the login page.
This is how we can use the SQLite database to perform CRUD (insert, update, delete and select) operations in android applications
to store and retrieve data from the SQLite database based on our requirements.
CONTACT US
Email: [email protected]
(mailto:[email protected])
https://www.tutlane.com/tutorial/android/android-sqlite-database-with-examples 6/6