Site icon WebArtDeveloper

How to Perform Select Operation in SQLite Database in Android

In this tutorial how to perform select 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/btnView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/btnAdd”
android:layout_below=”@+id/btnAdd”
android:layout_marginTop=”20dp”
android:onClick=”dbFunction”
android:text=”View” />

<Button
android:id=”@+id/btnViewAll”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBaseline=”@+id/btnView”
android:layout_alignBottom=”@+id/btnView”
android:layout_alignRight=”@+id/btnModify”
android:onClick=”dbFunction”
android:text=”View All” />

<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==btnView)
{
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())
{
editName.setText(c.getString(1));
editMarks.setText(c.getString(2));
}
else
{
showMessage(“Error”,”Invalid Roll No”);
clearText();
}
}
}
if(view==btnViewAll)
{
Cursor c=db.rawQuery(“SELECT * FROM student”, null);
if(c.getCount()==0)
{
showMessage(“Error”,”No records found”);
return;
}
else
{
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append(“Roll No: “+c.getString(0)+”\n”);
buffer.append(“Name: “+c.getString(1)+”\n”);
buffer.append(“Marks: “+c.getString(2)+”\n\n”);
}
showMessage(“Student Details”,buffer.toString());
}
}
}

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/ORUY8MXKZIc

Exit mobile version