Site icon WebArtDeveloper

How to Perform Insert Operation in SQLite Database in Android

In this tutorial how to create a new database and table in SQLite Database and how to perform insert operation in SQLite Database from an Android Application is shown.

SQLite database is inbuilt database of android.

It is included in android SDK.

It is lightweight database, so, only small amount of data can be stored in it.

To store the data in SQLite database there is no requirement of database drivers such as JDBC or ODBC drivers.

To perform the database operation we have various classes which belongs to android.database.sqlite package.

It has other classes such as DatabaseManagement and QueryBuilderHelper classes to perform databse operations.

SQLite database is an opensource Relational database.

Point to Remember:

Code:

activity_main.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” >

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”50dp”
android:text=”Student Details” />

<EditText
android:id=”@+id/editRollno”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/textView1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”29dp”
android:ems=”10″
android:hint=”Enter Roll No” >

<requestFocus />
</EditText>

<EditText
android:id=”@+id/editMarks”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/editName”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”34dp”
android:ems=”10″
android:hint=”Enter Marks” />

<EditText
android:id=”@+id/editName”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/editRollno”
android:layout_below=”@+id/editRollno”
android:layout_marginTop=”28dp”
android:ems=”10″
android:hint=”Enter Name” />

<Button
android:id=”@+id/btnAdd”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_below=”@+id/editMarks”
android:layout_marginLeft=”16dp”
android:layout_marginTop=”34dp”
android:onClick=”dbFunction”
android:text=”Add” />

<Button
android:id=”@+id/btnReset”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBottom=”@+id/btnViewAll”
android:layout_alignRight=”@+id/btnDelete”
android:onClick=”onReset”
android:text=”Reset” />

</RelativeLayout>

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity{

SQLiteDatabase db;
EditText editRollno,editName,editMarks;
Button btnAdd,btnModify,btnDelete,btnView,btnViewAll;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

db=openOrCreateDatabase(“StudDB”,Context.MODE_PRIVATE,null);
db.execSQL(“CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);”);

editRollno=(EditText)findViewById(R.id.editRollno);
editName=(EditText)findViewById(R.id.editName);
editMarks=(EditText)findViewById(R.id.editMarks);
btnAdd=(Button)findViewById(R.id.btnAdd);
}

public void dbFunction(View view)
{
if(view==btnAdd)
{
if(editRollno.getText().toString().trim().length()==0||editName.getText().toString().trim().length()==0||editMarks.getText().toString().trim().length()==0)
{
showMessage(“Error”,”Please enter all values”);
return;
}
else
{
db.execSQL(“INSERT INTO student VALUES(‘”+editRollno.getText()+”‘,'”+editName.getText()+”‘,'”+editMarks.getText()+”‘)”);
showMessage(“Success”,”Record added”);
clearText();
}
}
}

public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}

public void clearText()
{
editRollno.setText(“”);
editName.setText(“”);
editMarks.setText(“”);
editRollno.requestFocus();
}

public void onReset(View v)
{
editRollno.setText(“”);
editName.setText(“”);
editMarks.setText(“”);
editRollno.requestFocus();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

https://youtu.be/Ie0er54XwXs

Exit mobile version