articles

Home / DeveloperSection / Articles / Insert, Update, Delete in Android

Insert, Update, Delete in Android

Chris Anderson32225 28-Oct-2011

Android provides easy way to manage structured data in SQLite database. It has full support for SQLite database. Any databases you create will be accessible by name to any class in the application, but not outside the application.

In this article I am going to explain how perform DML operation (Insert, Update and Delete) in SQLite database in Android.

  •  Open res/values/strings.xml and add the following string element in the resource tag:
<string name="onClick">onClick</string>
  •    Start a new project named DMLDemo.
  •    Open res/layout/main.xml and insert the following:
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Buttonandroid:id="@+id/btnInsert"
              android:text="Insert"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="onClick"/>
    <Buttonandroid:id="@+id/btnUpdate"
              android:text="Update"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="onClick"/>
    <Buttonandroid:id="@+id/btnDelete"
              android:text="Delete"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="onClick"/>
</LinearLayout>
  •      Open the Activity file and insert the following code:
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
 
publicclass DatabaseActivity extends Activity {
   
private SQLiteDatabase db;
 
       private String table_name="StudentInfo";
       private String database_name="Test.db";
      
       publicstaticfinal String KEY_ROWID = "sid";
       publicstaticfinal String KEY_NAME = "name";
       publicstaticfinal String KEY_AGE = "age";
       publicstaticfinal String KEY_COURSE = "course";
      
    @Override
    publicvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        // create or open database file
        db = openOrCreateDatabase(database_name , SQLiteDatabase.CREATE_IF_NECESSARY, 
                                                                              
                                                                               null);
        db.setVersion(1);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);
       
        // creating table in database
        db.execSQL("CREATE TABLE IF NOT EXISTS "+table_name+" " +
                        "( sid INTEGER PRIMARY KEY AUTOINCREMENT," +
                        "  name TEXT," +
                        "  age INTEGER," +
                        "  course TEXT ); ");
    }
   
    // capture button id and perform function
    publicvoid onClick(View v)
    {
       switch(v.getId())
       {
              case R.id.btnInsert:
                     Insert("Rohit", "19", "BBA");
                     break;
              case R.id.btnUpdate:
                     Update(1, "Rohit", "20", "BCA");
                     break;
              case R.id.btnDelete:
                     Delete(1);
                     break;
       }
    }
   
    // inserting record in the database
    publicvoid Insert(String name, String age, String course)
    {
       ContentValues data=createContentValues(name, age, course);
       db.insert(table_name, null, data);
       Toast.makeText(this, "Record Inserted", Toast.LENGTH_SHORT).show();
    }
   
    // updating record in the database
    publicvoid Update(int sid, String name, String age, String course)
    {
       ContentValues updateValues = createContentValues(name, age,   course);
       db.update(table_name, updateValues, KEY_ROWID+"="+sid, null);
       Toast.makeText(this, "Record Updated", Toast.LENGTH_SHORT).show();
    }
   
    // deleting record rom the database
    publicvoid Delete(int sid)
    {
       db.delete(table_name, KEY_ROWID+"="+sid, null);
       Toast.makeText(this, "Record Deleted", Toast.LENGTH_SHORT).show();
    }
   
    // return a content of the database
    private ContentValues createContentValues(String name, String age, String course)     
    {
    {
              ContentValues values = new ContentValues();
              values.put(KEY_NAME, name);
              values.put(KEY_AGE, age);
              values.put(KEY_COURSE, course);
              return values;
    }
}
·         Run the application.

The output looks like below:

Insert, Update, Delete in Android

When user click on the Insert button record will be inserted in the database and update and delete operation also perform on their click.

You can check the database and its information by using adb shell in android. In order check the database information follow the steps given below:

1)      Run the cmd (command prompt).

2)      Go to the following location: d:\android-sdk_r13-windows\android-sdk-windows\platform-tools> (you have to user your own location).

3)      Type adb shell and press enter, a # symbol will display.

4)      Type sqlite3 /data/data/ [package name]/databases/database-name (Test.db) and press Enter.
For e.g. sqlite3 /data/data/com.android.databaseDemo/databases/Test.db

5)      Then type .tables in order to see the table present in the database.

6)      It will show you a list tables present in the database.



Updated 02-Apr-2020
hi I am software developer at mindstick software pvt. ltd.

Leave Comment

Comments

Liked By