First step: Create a class to inherit sqliteopenhelper.
The OnCreate method connects the database for the first time to execute
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/82/wKioL1V-rRmghKFcAARQMK5iU-g385.jpg "title=" 1.png " alt= "Wkiol1v-rrmghkfcaarqmk5iu-g385.jpg"/>
Step Two: Create the entity class person.
Package com.jasper.SQLite.entities;
public class Person {
private int id;
private String name;
private int age;
Public person () {
Super ();
}
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
public person (int ID, String name, int age) {
Super ();
This.id = ID;
THIS.name = name;
This.age = age;
}
@Override
Public String toString () {
return "person [id=" + ID + ", name=" + name + ", age=" + Age + "]";
}
}
Step three: Create a database operation class Persondao
Package Com.jasper.SQLite.dao;
Import java.util.ArrayList;
Import java.util.List;
Import Com.jasper.SQLite.db.PersonSQLiteOpenHelper;
Import Com.jasper.SQLite.entities.Person;
Import Android.content.Context;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;
public class Persondao {
Private Personsqliteopenhelper Mopenhelper;
Public Persondao (Context context) {
Mopenhelper = new Personsqliteopenhelper (context);
}
Inserting data into the persons table
public void Insert (person p) {
Sqlitedatabase db = Mopenhelper.getwritabledatabase ();
if (Db.isopen ()) {
Insert operation
Db.execsql ("INSERT into persons (Name,age) values (?,?);",
New object[] {p.getname (), P.getage ()});
Close the database
Db.close ();
}
}
public void Delete (int id) {
Sqlitedatabase db = Mopenhelper.getwritabledatabase ();
if (Db.isopen ()) {
Delete operation
Db.execsql ("Delete from persons where id =?;",
New integer[] {ID});
Close the database
Db.close ();
}
}
public void Update (String name, int id) {
Sqlitedatabase db = Mopenhelper.getwritabledatabase ();
if (Db.isopen ()) {
Update action
Db.execsql ("update persons set name =?") where id =?; ",
New object[] {name, id});
Close the database
Db.close ();
}
Db.close ();
}
Querying the entire sheet
Public list<person> Queryall () {
Sqlitedatabase db = Mopenhelper.getwritabledatabase ();
if (Db.isopen ()) {
Query operations
cursor cursor = db.rawquery ("Select id,name,age from Persons;",
NULL);
The result set is not empty and the entry is greater than 0
if (cursor! = NULL && cursor.getcount () > 0) {
list<person> personlist = new arraylist<person> ();
int id;
String name;
int age;
while (Cursor.movetonext ()) {
id = cursor.getint (0);
Name = cursor.getstring (1);
Age = Cursor.getint (2);
Personlist.add (new person (ID, name, age));
}
Close the database
Db.close ();
return personlist;
}
Db.close ();
}
return null;
}
Public person query (int id)
{
Sqlitedatabase db = Mopenhelper.getwritabledatabase ();
if (Db.isopen ()) {
cursor cursor = db.rawquery ("Select id,name,age from persons where id =?;", New String[]{id + ""});
if (cursor! = NULL && cursor.movetofirst ())
{
int _id = cursor.getint (0);
String name = cursor.getstring (1);
int age = Cursor.getint (2);
Db.close ();
return new person (_id, name, age);
}
Db.close ();
}
return null;
}
}
Fourth step: Create a Test class TestCase
Package com.jasper.SQLite.test;
Import java.util.List;
Import Com.jasper.SQLite.dao.PersonDao;
Import Com.jasper.SQLite.db.PersonSQLiteOpenHelper;
Import Com.jasper.SQLite.entities.Person;
Import Android.test.AndroidTestCase;
Import Android.util.Log;
public class TestCase extends Androidtestcase {
private static final String TAG = "TestCase";
public void Test ()
{
Personsqliteopenhelper openhelper = new Personsqliteopenhelper (GetContext ());
Openhelper.getwritabledatabase ();
}
public void Testinsert ()
{
Persondao per = new Persondao (GetContext ());
Per.insert (new person (0, "punk", 20));
}
public void Testdelete ()
{
Persondao per = new Persondao (GetContext ());
Per.delete (2);
}
public void Testupdate ()
{
Persondao per = new Persondao (GetContext ());
Per.update ("Lily", 1);
}
public void Testqueryall ()
{
Persondao per = new Persondao (GetContext ());
list<person> personlist = Per.queryall ();
for (person Person:personlist) {
LOG.I (TAG, person.tostring ());
}
}
public void Testquery ()
{
Persondao per = new Persondao (GetContext ());
Person person = per.query (1);
LOG.I (TAG, person.tostring ());
}
}
SQLite additions and deletions to search