Site icon WebArtDeveloper

How to Perform Update Operation in SQLite Database in Android

In this tutorial how to perform update operation in SQLite Database from  an Android Application is shown.

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/btnModify”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBaseline=”@+id/btnAdd”
android:layout_alignBottom=”@+id/btnAdd”
android:layout_alignRight=”@+id/textView1″
android:onClick=”dbFunction”
android:text=”Modify” />

<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==btnModify)
{
if(editRollno.getText().toString().trim().length()==0)
{
showMessage(“Error”,”Please enter Roll No”);
return;
}
else
{
Cursor c=db.rawQuery(“SELECT * FROM student WHERE rollno='”+editRollno.getText()+”‘”, null);
if(c.moveToFirst())
{
db.execSQL(“UPDATE student SET name='”+editName.getText()+”‘,marks='”+editMarks.getText()+”‘ WHERE rollno='”+editRollno.getText()+”‘”);
showMessage(“Success”,”Record Modified”);
}
else
{
showMessage(“Error”,”Invalid Roll No”);
}
}
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/toNiYhcQo64

Exit mobile version