How to Manipulate Data from Sqlite Database in Android

Howdy all, today I am going to show you how to CRUD(Create ,Read,Update,Delete) in Android Application in a simplest way, this is very awesome tutorial and simple one. Every one specially for novice who think that Database is very difficult.So without wasting time lets dig inside-

Sqlite is a RDBMS contained in a C programming library functions.Download Code

Cursorc=db.rawQuery("SELECT * FROM student WHERE rollno='"+editstu_Rollno.getText()+"'",null);

if(c.moveToFirst())

{

//view with the help of cursor object

stu_editName.setText(c.getString(1));

stu_editMarks.setText(c.getString(2));

}

else

{

alert("Error","Invalid Rollno");

clearText();

}

}

if(view==btnViewAll)

{

Cursorc=db.rawQuery("SELECT * FROM student",null);

if(c.getCount()==0)

{

alert("Error","No records found");

return;

}

StringBuffer buffer=newStringBuffer();

while(c.moveToNext())

{

//view all

buffer.append("Rollno: "+c.getString(0)+"\n");

buffer.append("Name: "+c.getString(1)+"\n");

buffer.append("Marks: "+c.getString(2)+"\n\n");

}

alert("Student Details",buffer.toString());

}

if(view==btnShowInfo)

{

alert("Simple Sqlite in Android","Developed By AndroidArena.co.in");

}

}

publicvoidalert(Stringtitle,Stringmessage)

{

Builder builder=newBuilder(this);

builder.setCancelable(true);

builder.setTitle(title);

builder.setMessage(message);

builder.show();

}

publicvoidclearText()

{

editstu_Rollno.setText("");

stu_editName.setText("");

stu_editMarks.setText("");

editstu_Rollno.requestFocus();

}

}

In the above code I have made two methods for showing messages on Alertbox and other is ClearText for clearing text after performing Records.

In this my column names are –

Rollno

Name

Marks

With VARCHAR datatype – You can set PRIMARY KEY later on.

In this I have just create Database and create Table and then perform simple if else condition to perform SQLITE DATABASE CRUD.This is simple and minor Project based on Local Database connectivity for Student Management System in Android.

Now you can Run your project So Practice Hard and learn as much you can :p