working with SQLite database in android

We’ve created most of the Android UI design, but did you know that when you want to store a large amount of data as a text file, you’ll need a database.

Now, to store a large number of data into the database.

So we will know a little about the database connectivity on Android step by step.

Which database is talked about in this tutorial?

In the market, there are lots of databases available.

Some databases are offline or some online.

In this tutorial, we will store the database in a local server mean your device.

Of course, we’ll be storing data in the local device.

SQLite is the best database for storing local devices. So we’ll use SQLite database.

What is SQLite Database ?

SQLite database is lightweight and open source relational database that storing inside the device.

Working with SQLite database

SQLite database methods and classes?

We are going to see SQLite classes, function, constructor and methods.

We’ll create data, read data, update data and delete data to perform the task.

SQLiteOpenHelper :  class to manage database creation and version management.

onCreate : this called when you create first time database.

For example

    public void onCreate(SQLiteDatabase db) {
String sql="create table thing(id integer primary key,name text)";

onUpgrade : This method called when the database required to be upgraded. Whenever you need to upgraded to the new schema version.

Also called when drop tables, add tables, or do anything.

For example 

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql="drop table if exists thing";


Used to execute table or query.


For storing column values in the database.

For example

ContentValues cv=new ContentValues();

In short, you can store name in ContentValue.


Delete all values.

When you log in or enter the information in the EditText, then click on the button at that time called clear method, then all the entered values are cleared. Clear (); method is then used.

When you fill in the login information the password default is removed if you typed the wrong password.

For example

private void clear() {

Cursor : we’ll create a cursor to retrieve data from the database. We use a select statement to fetch data from a SQLite table. At that time, the database creates a cursor.

In short, the cursor stores temporary results.

For example

Cursor cursor = sqLiteDatabase.rawQuery("select *from thing", null);


  The getData();  Method returns the data buffer which is used to receive or send data.


Generally, this method is used to move the cursor to the first row.


You can used this method moves the cursor to the next row.


Using this, you can retrieve a string value.

For example



Use for confirmation that column will exist or not.


In SQLiteDatabase we can create or open database using getReadableDatabase();

For example

        sqLiteDatabase = dBmain.getReadableDatabase();


Used to read and write databases that have been created or opened.

For example



ArrayList is a dynamic data structure. Using ArrayList you can add or remove elements that are stored in sequence.

You can import the package java .util.


If you want to display text into TextView, then we’ll use setText() to setText in TextView.

For example

    private void UserData() {
    edtname.setText("course code");


This method returns an Intent object. Using this you can pass data from one activity to another.

Also Read : ListView with selected background…

If you want to display usename after login then getIntent() call then getExtras() method is invoked.

 void editData() {
    if (getIntent().getBundleExtra("userdata")!=null){
    Bundle bundle=getIntent().getBundleExtra("userdata");


For adds extended data to the intent.

Intent intent = new Intent(MainActivity.this, MainActvity2.class);   
  String username  = null;
  intent.putExtra(struser, username );


Bundle bundle = getIntent().getExtras();

        if(bundle.getString("struser")!= null)
            // setText() on userName 


For fetching data which you added using putExtra();

set data

String username = "course code";
Intent intent = new Intent(getApplicationContext(), MainActivity2.class);
intent.putExtra("name", username);

get data

String username;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    username= bundle.getString("name");


This class is used to access resources, databases, share preferences.

For SQLite Database Related videos, watch on YouTube

Leave a Reply