-
First we create a database help class personsqlliteopenhelper, word spelling a little bit wrong.
Let this class inherit Sqliteopenhelper
-
package com.example.android03db.db;import android.content.context;import Android.database.sqlite.sqlitedatabase;import android.database.sqlite.sqlitedatabase.cursorfactory;import android.database.sqlite.SQLiteOpenHelper;/** * database help classes for creating and managing databases * @author Administrator * */public class personsqlliteopenhelper extends sqliteopenhelper {/** * Database Constructors * @param context * @param name database name * @param factory Cursor Engineering * @param version database version */public personsqlliteopenhelper ( Context context, string name,cursorfactory factory, int version) {super ( context, "Hzg01.db", null, 1);//context, database name, version, cannot be less than 1}public personsqlliteopenhelper ( Context context) {super (context, "hzg01.db", null, 1);//context, database name, version, cannot be less than 1}/** * call this method at the first time the database is created * initialize some tables */@Overridepublic void oncreate (SQLITEDATABASE DB) {//Operational Database String sql= "create Table person (Id integer primary key,name varchar (), Age integer) ";d B.execsql ( SQL),//Create a person table}/** * the database version number is updated when you call this method */@Overridepublic void onupgrade ( sqlitedatabase db, int oldversion, int newversion) {}}
Next we write a DAO
Package com.example.android03db.dao;import java.util.arraylist;import java.util.list;import android.content.Context;import android.database.Cursor;import Android.database.sqlite.sqlitedatabase;import com.example.android03db.db.personsqlliteopenhelper;import com.example.android03db.entities.Person;public class PersonDao {private Help class Public persondao for personsqlliteopenhelper personsql;//database (context context) { personsql= New personsqlliteopenhelper (context);} /** * added to the person table a data * @param p */public void insert (person p) { Sqlitedatabase db=personsql.getwritabledatabase (); if (Db.isopen ()) {//If database open perform add action//execute Add to database action string sql= "insert into person (name,age) values (?,?)"; Db.execsql (Sql,new object[]{p.getname (), P.getage ()});d b.close ();//Database close}}public void delete (int id) {Sqlitedatabase db=personsql.getwritabledatabaSE ();//Gets a writable database object if (Db.isopen ()) {///If the database opens to perform the added action//perform the operation added to the database Db.execsql ("delete from person Where id= ", new integer[]{id});d b.close ();//Database Close}}public void update (Int id, String name) {sqlitedatabase db=personsql.getwritabledatabase (); if (Db.isopen ()) {//If the database opens to perform the added action// Perform the action added to the database Db.execsql ("Update person set name=? where id=?", new object[]{ Name,id});d b.close ();//Database off}}public list<person> queryall () {sqlitedatabase db= Personsql.getreadabledatabase ();//Get a read-only database object if (Db.isopen ()) {//db.execsql ("Select *from person"); Cursor cursor=db.rawquery ("Select id,name,age from person", null); if (cursor!=null &&cursor.getcount () >0) {list<person> persons=new arraylist<person> (); while ( Cursor.movetonext ()) {int id=cursor.getint (0);//Take No. 0 column idstring name=cursor.getstring (1); int age= Cursor.getint (2);p Ersons.add (new pErson (Id,name,age));} Db.close (); return persons;} Db.close ();} Return null;} Public person queryintem (int id) {sqlitedatabase db=personsql.getreadabledatabase ();// Get a read-only database object if (Db.isopen ()) {Cursor cursor=db.rawquery ("Select id,name,age from person where id= ", new string[]{id+" "}); if (Cursor!=null&&cursor.movetofirst ()) {int pid=cursor.getint (0);//Take No. 0 column idstring name=cursor.getstring (1); Int age=cursor.getint (2); return new person (Pid,name,age);}} Return null;}}
Define an entity class person
package com.example.android03db.entities;public class person {private int id; Private string name;private int age;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 age) {this.age = age;} Public person (Int id, string name, int age) {super (); this.id = id ; this.name = name;this.age = age;} Public person () {super ();} Public person (String name, int age) {super (); this.name = name;this.age = age;} @Overridepublic string tostring () {return "person [id=" + id + ", name= "&Nbsp;+ name + ", age=" + age + "]";}}
Test phase
package com.example.android03db.test;import java.util.list;import android.test.androidtestcase; import android.util.log;import com.example.android03db.dao.persondao;import Com.example.android03db.db.personsqlliteopenhelper;import com.example.android03db.entities.person;public class test extends androidtestcase{private static final string tag = "Test";p ublic static void main (String[] args) {// TODO Auto-generated method stub}public void t () {personsqlliteopenhelper pl=new Personsqlliteopenhelper (GetContext ());//When the database is first connected, the database file OnCreate is called Pl.getreadabledatabase ();} Public void testinsert () {Persondao dao=new persondao (GetContext ());d Ao.insert (new Person ("Zhang San", 25));} Public void testdelete () {Persondao dao=new persondao (GetContext ());d ao.delete (1);} Public void testupdate () {persondao dao=New persondao (GetContext ());d ao.update (2, "John Doe");} Public void testqueryall () {Persondao dao=new persondao (GetContext ()); List<person> persons=dao.queryall (); for (person ps:persons) {log.i (tag, ps.tostring ());}} Public void testqueryitem () {Persondao dao=new persondao (GetContext ()); Person p=dao.queryintem (2); LOG.I (Tag, p.tostring ());}}
This article is from the "Java White Battlefield" blog, be sure to keep this source http://8023java.blog.51cto.com/10117207/1669304
Search and delete of SQLite in Android