working with SQLite database in android

  • Post comments:0 Comments

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

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

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 

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql="drop table if exists thing";
db.execSQL(sql);
onCreate(db);
    }

db.execSQL();

Used to execute table or query.

ContentValues()

For storing column values in the database.

For example

ContentValues cv=new ContentValues();
cv.put(Name,username);

In short, you can store name in ContentValue.

Clear();

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() {
    edtname.setText("");
    }

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);

getData();

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

moveToFirst();

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

moveToNext();

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

getString(int);

Using this, you can retrieve a string value.

For example

    edtname.setText(bundle.getString("name"));

getColumnIndex()

Use for confirmation that column will exist or not.

getReadableDatabase();

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

For example

        sqLiteDatabase = dBmain.getReadableDatabase();

getWritableDatabase();

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

For example

                sqLiteDatabase=dBmain.getWritableDatabase();

ArrayList

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.

setText()

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");

getIntent();

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");
    id=bundle.getInt("id");
    edtname.setText(bundle.getString("name"));
    }
    }

putExtra();

For adds extended data to the intent.

MainActivity.java

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

And MainActivity2.java

Bundle bundle = getIntent().getExtras();

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

getExtra();

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);
startActivity(intent);

get data

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

Context

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

For SQLite Database Related videos, watch on YouTube

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments