Core code DAO Class
Package Com.examp.use_sqlite.dao;import Java.util.arraylist;import Java.util.list;import Android.content.contentvalues;import Android.content.context;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Com.examp.use_sqlite.personsqliteopenhelper;import Com.examp.use_ Sqlite.domain.person;public class Persondao {//Gets the database object to manipulate private personsqliteopenhelper helper;/** * Completion of helper initialization in the construction method * * @param context */public Persondao (context context) {helper = new Personsqliteopenhelper (context);} /** * Add Data * * @param name name * @param number phone */public void Add (string name, String #) {//Gets the connection object of the database Sqlitedatabase db = Helper.getwritabledatabase ();//execute an SQL statement, not a query/insert/update/delete. Db.execsql ("INSERT into person (name,number) VALUES (?,?)",//New object[] {name, number});//The following is the API recommended notation Contentvalue s values = new Contentvalues (); Values.put ("name", name), Values.put ("number", number);d B.insert ("person", null, values) ;//Close database connection Db.close () in a timely manner;} /** * Query data by user name * * @param name * Query condition * @return Query result */public person Findbyname (String name) {//Get database connection Sqlitedatabase db = Helpe R.getreadabledatabase ();//Execute database Operation statement,//Get cursor cursor instance, that is, result set, similar to Javaweb in resultsetcursor cursor = Db.rawquery (" SELECT * from person where name=? ", new string[] {name});//Declare result object person person = null;//Determine if there is a result return if (Cursor.movetonext ( ) {////Cursor.getcolumnindex ("id") of the field of a single row of data in the dataset, gets the subscript//Cursor.getint (index) of the specified field, or GetString (index); Is the data that gets the specified subscript index to int type; int id = cursor.getint (cursor.getcolumnindex ("id")); String named = cursor.getstring (Cursor.getcolumnindex ("name")); String number = cursor.getstring (Cursor.getcolumnindex ("number"));//Instantiate query result data Object person = new person (ID, named, number);} Close the database connection, release the Resource Db.close ();//Return the data result object return person; /** * Update a data according to name * * @param name * with new conditions * @param newnumber * New data */public void update (String nam E, String newnumber) {sqlitedatabase db = Helper.getwritabledatabase ();d b.execsql ("Update person set NumbeR=? Where Name=? ", new object[] {newnumber, name});d B.close (); /** * Delete Data according to the specified name * * @param name * Delete condition * */public void Delete (String name) {Sqlitedatabase db = HELPER.GETW Ritabledatabase ();d b.execsql ("Delete from person where name=?", new object[] {name});d b.close ();} Public list<person> FindAll () {Sqlitedatabase db = Helper.getreadabledatabase (); cursor cursor = db.rawquery ("SELECT * from person", null);//Declare result set//Initialize result set list<person> persons = new Arraylist<p Erson> ();//Determine if there is a result return while (Cursor.movetonext ()) {//Declare result object person man = null;//take out data from a single row of data in the dataset// Cursor.getcolumnindex ("id"), gets the subscript//Cursor.getint (index) of the specified field, or GetString (index); Is the data that gets the specified subscript index to int type; int id = Cursor.getint (Cursor.getcolumnindex ("id")); String named = cursor.getstring (Cursor.getcolumnindex ("name")); String number = cursor.getstring (Cursor.getcolumnindex ("number"));//instantiation of query result data Object person = new person (ID, named, number) ;//Add Data Persons.add (person) to the result set;} Close the connection, release the Resource Db.close ();//Put back the resultsSet return persons;}}
Other auxiliary classes used
Package Com.examp.use_sqlite;import Android.content.context;import Android.database.sqlite.sqlitedatabase;import android.database.sqlite.sqliteopenhelper;/** * Database Created class * * @author Martindong * */public class Personsqliteopenhelper extends Sqliteopenhelper {/** * Database construction method, used to define: Database environment, database name, database query results and database version * * @param Context */public Personsqliteopenhelper (context context) {Super (context, "person.db", NULL, 1);} /** * Method Called when the database was first created * * @param db created database */@Overridepublic void OnCreate (Sqlitedatabase db) {//Initialize the table structure of the database Db.exe Csql ("CREATE TABLE person (ID integer PRIMARY key autoincrement,name varchar (), number varchar (20))"); @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {}}
Entity Business Bean
Package Com.examp.use_sqlite.domain;public class Person {private int id;private string Name;private string number;public Person () {}public person (int ID, string name, string number) {this.id = Id;this.name = Name;this.number = number;} 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 String GetNumber () {return number;} public void Setnumber (String number) {this.number = number;} @Overridepublic String toString () {return "person [id=" + ID + ", name=" + name + ", number=" + number+ "]";}}
Test class
Package Com.examp.use_sqlite.test;import Java.util.list;import Com.examp.use_sqlite.personsqliteopenhelper;import Com.examp.use_sqlite.dao.persondao;import Com.examp.use_sqlite.domain.person;import Android.test.AndroidTestCase ;p ublic class Testperson extends Androidtestcase {public void Testcreatedb () throws Exception {Personsqliteopenhelper hel per = new Personsqliteopenhelper (GetContext ()); Helper.getwritabledatabase ();} public void Testadd () {Persondao dao = new Persondao (GetContext ());d Ao.add ("Donghongyu", "1231231");} public void Testfindbyname () {Persondao dao = new Persondao (GetContext ()); Person person = dao.findbyname ("Lisi"); System.out.println (Person.tostring ());} public void Testupdate () {Persondao dao = new Persondao (GetContext ());d ao.update ("Lisi", "11111111111");} public void Testdelete () {Persondao dao = new Persondao (GetContext ());d ao.delete ("Lisi");} public void Testfindall () {Persondao dao = new Persondao (GetContext ()); list<person> persons = Dao.findall (); for (person PERson:persons) {System.out.println (person);}}}
Note: When testing with junit, you need to add the following settings in the permissions file androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.examp.use_SQLite "android:versioncode=" 1 "android:versionname=" 1.0 "> <instrumenta tion android:name= "Android.test.InstrumentationTestRunner" android:targetpackage= "Com.examp.use_SQLite"/> ; <USES-SDK android:minsdkversion= "8" android:targetsdkversion= "/> <application android" : allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:the Me= "@style/apptheme" > <uses-library android:name= "Android.test.runner"/> <activity Android:name= "com.examp.use_SQLite.MainActivity" android:label= "@string/app_name" > <intent-fi lter> <action android:name= "Android.intent.action.MAIN"/> <category Android:nam E= "Android.intent.category.LAUNCHER "/> </intent-filter> </activity> </application></manifest>