Android Primer 006 (SQLite additions and deletions)
PackageCn.rfvip.feb_14_2_sqlite;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;ImportAndroid.database.sqlite.SQLiteOpenHelper; Public classMyopenhelperextendsSqliteopenhelper { PublicMyopenhelper (Context context, String name, Cursorfactory factory,intversion) { Super(context, name, Factory, version); //TODO Auto-generated constructor stub} @Override Public voidonCreate (Sqlitedatabase db) {//TODO Auto-generated method stubsSYSTEM.OUT.PRINTLN ("CREATE DATABASE"); Db.execsql ("CREATE TABLE people (_id integer PRIMARY KEY autoincrement,name char (Ten), Salary char (), phone Integer (20))"); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { //TODO Auto-generated method stubsSYSTEM.OUT.PRINTLN ("Update Database"); }}
PackageCn.rfvip.feb_14_2_sqlite. UnitTest;ImportCn.rfvip.feb_14_2_sqlite. Myopenhelper;ImportAndroid.content.res.ObbInfo;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.test.AndroidTestCase; Public classUnitTestextendsAndroidtestcase {PrivateMyopenhelper Myopenhelper;PrivateSqlitedatabase db;//called before testing@Overrideprotected voidSetUp ()throwsException {//TODO Auto-generated method stubs Super. SetUp (); Myopenhelper=NewMyopenhelper (GetContext (), "people.db",NULL, 2); DB=myopenhelper.getwritabledatabase ();}//called after testing@Overrideprotected voidTearDown ()throwsException {//TODO Auto-generated method stubs Super. TearDown (); Db.close (); } Public voidTest () {Myopenhelper=NewMyopenhelper (GetContext (), "people.db",NULL, 2); Sqlitedatabase DB=myopenhelper.getwritabledatabase (); } Public voidInsert () {Db.execsql (Insert into people (Name,salary,phone) VALUES (?,?,?),Newobject[]{"Chun visit 1", "15000", 180178}); Db.execsql (Insert into people (Name,salary,phone) VALUES (?,?,?),Newobject[]{"Chun visit 2", "15000", 180178}); Db.execsql (Insert into people (Name,salary,phone) VALUES (?,?,?),Newobject[]{"Chun visit 3", "15000", 180178}); Db.execsql (Insert into people (Name,salary,phone) VALUES (?,?,?),Newobject[]{"Chun visit 4", "15000", 180178}); Db.execsql (Insert into people (Name,salary,phone) VALUES (?,?,?),Newobject[]{"Chun visit 5", "15000", 180178}); } Public voidDelete () {Db.execsql ("Delete from people where name=?",Newobject[]{"Chun Visit"}); } Public voidUpdate () {Db.execsql ("Update people set phone=?" Where Name=? ",Newobject[]{153333, "Chun visit"}); } Public voidSelect () {cursor cursor=db.rawquery ("Select name,salary from People",NULL); while(Cursor.movetonext ()) {String name=cursor.getstring (Cursor.getcolumnindex ("name")); String Salary=cursor.getstring (Cursor.getcolumnindex ("salary")); SYSTEM.OUT.PRINTLN (Name+ ";" +salary); } }}
Google package additions and deletions to search
//How to package Google Public voidInsertapi () {contentvalues values=Newcontentvalues (); Values.put ("Name", "Li degree"); Values.put ("Salary", "20000"); Values.put ("Phone", "15000"); Long Numberrow= Db.insert ("People",NULL, values); System.out.println ("Inserted current number of rows:" +Numberrow); } Public voidDeleteapi () {intNumberrow = Db.delete ("People", "name=?") And _id=? ",Newstring[] {"Chun visit 1", "2" }); System.out.println ("Number of rows deleted:" +Numberrow); } Public voidUpdateapi () {contentvalues values=Newcontentvalues (); Values.put ("Name", "Lidu"); intnumberrow= db.update ("People", Values, "Name=?") And _id=? ",Newstring[]{"Chun visit 2", "3" } ); System.out.println ("Number of rows updated:" +Numberrow); } //columns Null to query all new string[]{"name", "salary"} Public voidSelectapi () {cursor cursor=db.query ("People",NULL,NULL,NULL,NULL,NULL,NULL); while(Cursor.movetonext ()) {String name=cursor.getstring (Cursor.getcolumnindex ("name")); String Salary=cursor.getstring (Cursor.getcolumnindex ("salary")); String Phone=cursor.getstring (Cursor.getcolumnindex ("Phone")); System.out.println ("Name:" + name + "; Salary:" + salary + "; Phone:" +phone); } }
Android Primer 006 (SQLite additions and deletions)