Knowledge Point Detailed: http://blog.csdn.net/zhaoyazhi2129/article/details/9026093
Mainactivity.java,user.java,basedao.java,userdao.java Ibid. http://blog.csdn.net/zhaoyazhi2129/article/details/ 28640195userdaoimple.java
Package Com.example.android_sqlite.dao.impl;import Java.util.arraylist;import Java.util.list;import Android.content.contentvalues;import Android.database.cursor;import android.database.sqlite.SQLiteDatabase; Import Com.example.android_sqlite.dao.userdao;import Com.example.android_sqlite.database.databasehelper;import Com.example.android_sqlite.domain.users;public class Userdaoimpl implements Userdao {//Dependent object Private Databasehelper DH ;//Instantiate public Userdaoimpl (Databasehelper databasehelper) {THIS.DH = Databasehelper through the constructor of the database;} @Overridepublic Boolean insert (Users entity) {///define return value Boolean flag = false;//Get database operand sqlitedatabase db = dh.getwritabled Atabase ();//package parameters contentvalues values = new Contentvalues () values.put ("userid", Entity.getuserid ()); Values.put (" Username ", Entity.getusername ()), Values.put (" Userage ", Entity.getuserage ()), Values.put (" Usersalary ", Entity.getusersalary ());///Parameter 1: Table name, Parameter 2: The name of the default column, Parameter 3: Insert data Long id = Db.insert ("Users", null, values);//The return value of the insert is judged if (ID!). =-1) {flag = TRUE;} return flag;} @Overridepublic Boolean update (Users entity) {///Definition return value Boolean flag = false;//Get database operand sqlitedatabase db = dh.getwritabled Atabase ();//package parameters contentvalues values = new Contentvalues () values.put ("userid", Entity.getuserid ()); Values.put (" Username ", Entity.getusername ()), Values.put (" Userage ", Entity.getuserage ()), Values.put (" Usersalary ", Entity.getusersalary ()); int num = Db.update ("Users", values, "Userid=?", new string[] {Entity.getuserid () + ""});//Return to insert return value to determine if (num > 0) {flag = true;} return flag;} @Overridepublic Boolean Delete (Users entity) {return Deletebyid (Entity.getuserid ());} @Overridepublic Boolean Deletebyid (Integer ID) {//Definition return value Boolean flag = false;//Get database operand sqlitedatabase db = Dh.getwritabl Edatabase ();///Parameter 1: Table name, Parameter 2: Default column name, Parameter 3: insert data int num = Db.delete ("Users", "Userid=?", new string[] {id + ""});//delete all//int num = Db.delete ("Users", null,null);//The return value of the insert is judged if (num > 0) {flag = true;} return flag;} @Overridepublic users FindByID (Integer id) {users enTity = null; Sqlitedatabase db = Dh.getwritabledatabase (); Cursor C = db.query ("Users", new string[] {"userid", "username", "userage", "Usersalary"}, "Userid=?", new string[] {ID + ""}, NULL, NULL, NULL), if (C.movetonext ()) {entity = new Users (); Entity.setuserid (C.getint (C.getcolumnindex ("userid")) ); Entity.setusername (C.getstring (C.getcolumnindex ("userid")); Entity.setuserage (C.getint (C.getcolumnindex (" Userage ")); Entity.setusersalary (C.getdouble (C.getcolumnindex (" Usersalary "))); return entity;} @Overridepublic list<users> FindAll () {list<users> entities = new arraylist<users> (); Sqlitedatabase db = Dh.getwritabledatabase (); Cursor C = db.query ("Users", new string[] {"userid", "username", "userage", "Usersalary"}, NULL, NULL, NULL, NULL, NULL); while (C.movetonext ()) {Users entity = new Users (); Entity.setuserid (C.getint (C.getcolumnindex ("userid")); Entity.setusername (C.getstring (C.getcolumnindex ("userid")); Entity.setuserage (C.getint (C.getcolumnindex (" Userage ")); entity.Setusersalary (C.getdouble (C.getcolumnindex ("usersalary")); Entities.add (entity);} return entities;} public void Transaction () {Sqlitedatabase db = Dh.getreadabledatabase ();//Start Transaction db.begintransaction (); try {db.execsql (" Update users set usersalary=? Where userid=? ", new object[] {, one});d B.execsql (" Update users set usersalary=? "). Where userid=? ", new object[] {);d b.settransactionsuccessful ();//Set the transaction flag to be successful, provide the transaction at the end of the transaction, or rollback the transaction} catch ( Exception e) {e.printstacktrace ();} finally {//if the transaction was not successfully rolled back db.endtransaction ();}}}
Databasetese.java
Package Com.example.android_sqlite.test;import Java.util.list;import Com.example.android_sqlite.dao.UserDao; Import Com.example.android_sqlite.dao.impl.userdaoimpl;import Com.example.android_sqlite.database.DatabaseHelper ; Import Com.example.android_sqlite.domain.users;import Android.test.androidtestcase;public class DatabaseTese Extends Androidtestcase {public void CreateDatabase () {databasehelper DH = new Databasehelper (GetContext ()); Dh.getwritabledatabase ();} public void Insert () {Databasehelper databasehelper = new Databasehelper (GetContext ()); Userdao Userdao = new Userdaoimpl (databasehelper); Users entity = new users (null, "Zhangsan", 4000.0); Boolean flag = Userdao.insert (entity); Assertequals (true, flag);} public void Update () {Databasehelper databasehelper = new Databasehelper (GetContext ()); Userdao Userdao = new Userdaoimpl (databasehelper); Users entity = new users (8, "AAA", + 3000.0); Boolean flag = Userdao.update (entity); Assertequals (true, flag);} public void Delete () {DatabaseheLper databasehelper = new Databasehelper (GetContext ()); Userdao Userdao = new Userdaoimpl (databasehelper); Users entity = new users (6, "", NULL, NULL); Boolean flag = Userdao.delete (entity); Assertequals (true, flag);} public void FindByID () {Databasehelper databasehelper = new Databasehelper (GetContext ()); Userdao Userdao = new Userdaoimpl (databasehelper); Users entity = Userdao.findbyid (3); if (entity! = null) {System.out.println (entity.tostring ());} else { SYSTEM.OUT.PRINTLN ("no Message");}} public void FindAll () {Databasehelper databasehelper = new Databasehelper (GetContext ()); Userdao Userdao = new Userdaoimpl (databasehelper); list<users> entities = Userdao.findall (); for (Users entity:entities) {System.out.println (entity.tostring ());}} How transactions are handled public void Testtransaction () {Databasehelper databasehelper = new Databasehelper (GetContext ()); Userdaoimpl Userdaoimpl = new Userdaoimpl (databasehelper); Userdaoimpl.transaction (); } }