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.
<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">
<Button android:id="@+id/btnInsert"
android:text="Insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"/>
<Button android:id="@+id/btnUpdate"
android:text="Update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"/>
<Button android: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;
public class DatabaseActivity extends Activity {
private SQLiteDatabase db;
private String table_name="StudentInfo";
private String database_name="Test.db";
public static final String KEY_ROWID = "sid";
public static final String KEY_NAME = "name";
public static final String KEY_AGE = "age";
public static final String KEY_COURSE = "course";
@Override
public void 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
public void 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
public void 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
public void 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
public void 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:

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.
Leave Comment
2 Comments