Version 1.0 Code:
Packagecom.swust.sqlitedatabase.test;ImportCom.swust.sqlitedatabase.myOpenHelper;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.test.AndroidTestCase; Public classTestCaseextendsAndroidtestcase { Public voidTest () {//The first parameter:this-> is now applied without booting, no, real time with this. So get the virtual context GetContext ()//second parameter: Database file//Fourth parameter: Version number//The recno field is systematic, ignoringMyopenhelper moh =NewMyopenhelper (GetContext (), "people.db",NULL, 1); //If the database does not exist, create the database first, and then get the readable writable database object, if the data exists, open it directlySqlitedatabase db=moh.getwritabledatabase (); //The created object is also readable and writable, and if the storage space is full, the read-only database object is returned//sqlitedatabase db=moh.getreadabledatabase (); } Public voidInsert () {myopenhelper moh=NewMyopenhelper (GetContext (), "people.db",NULL, 1); Sqlitedatabase DB=moh.getwritabledatabase (); //database char, double, etc. are all strings because this lightweight data does not detect dataDb.execsql ("INSERT into person (Name,salary,phone) VALUES (?,?,?)",Newobject[]{"King * *", "25.34", 12.345}); Db.execsql (Insert into person (Name,salary,phone) VALUES (?,?,?),Newobject[]{"Wang * * University", 25.34, "12.345"}); Db.execsql (Insert into person (Name,salary,phone) VALUES (?,?,?),Newobject[]{"King * *", "25.34", 12.345}); Db.execsql (Insert into person (Name,salary,phone) VALUES (?,?,?),Newobject[]{"Wang * * University", 25.34, "12.345"}); Db.close (); }}
After insertion:
Delete:
Public void Delete () { newnull, 1); Sqlitedatabase db=moh.getwritabledatabase (); Db.execsql ("Delete from person where name =?",new object[]{"King * *"}) ;
After that, the field named "King * *" was deleted
Effect:
-------------------------------------
Code not optimized:
Packagecom.swust.sqlitedatabase.test;ImportCom.swust.sqlitedatabase.myOpenHelper;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.test.AndroidTestCase; Public classTestCaseextendsAndroidtestcase { Public voidTest () {//The first parameter:this-> is now applied without booting, no, real time with this. So get the virtual context GetContext ()//second parameter: Database file//Fourth parameter: Version number//The recno field is systematic, ignoringMyopenhelper moh =NewMyopenhelper (GetContext (), "people.db",NULL, 1); //If the database does not exist, create the database first, and then get the readable writable database object, if the data exists, open it directlySqlitedatabase db=moh.getwritabledatabase (); //The created object is also readable and writable, and if the storage space is full, the read-only database object is returned//sqlitedatabase db=moh.getreadabledatabase (); } Public voidInsert () {myopenhelper moh=NewMyopenhelper (GetContext (), "people.db",NULL, 1); Sqlitedatabase DB=moh.getwritabledatabase (); //database char, double, etc. are all strings because this lightweight data does not detect dataDb.execsql ("INSERT into person (Name,salary,phone) VALUES (?,?,?)",Newobject[]{"King * *", "25.34", 12.345}); Db.execsql (Insert into person (Name,salary,phone) VALUES (?,?,?),Newobject[]{"Wang * * University", 25.34, "12.345"}); Db.execsql (Insert into person (Name,salary,phone) VALUES (?,?,?),Newobject[]{"King * *", "25.34", 12.345}); Db.execsql (Insert into person (Name,salary,phone) VALUES (?,?,?),Newobject[]{"Wang Shuqing University", 25.34, "12.345"}); Db.close (); } Public voidDelete () {myopenhelper moh=NewMyopenhelper (GetContext (), "people.db",NULL, 1); Sqlitedatabase DB=moh.getwritabledatabase (); Db.execsql ("Delete from person where name =?",Newobject[]{"King * *"}); }}
After optimization:
Packagecom.itheima.sqlitedatabase.test;ImportCom.itheima.sqlitedatabase.MyOpenHelper;Importandroid.content.ContentValues;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.test.AndroidTestCase; Public classTestCaseextendsAndroidtestcase {//The test framework is not initialized at this time, there is no virtual context object//private Myopenhelper Oh = new Myopenhelper (GetContext (), "people.db", NULL, 1); PrivateMyopenhelper Oh; PrivateSqlitedatabase DB; Public voidTest () {//getcontext (): Gets a virtual contextMyopenhelper Oh =NewMyopenhelper (GetContext (), "people.db",NULL, 1); //If the database does not exist, create the database first, and then get the readable writable database object, if the database exists, open it directlySqlitedatabase db =oh.getwritabledatabase (); //returns a read-only database object if the storage space is full//Sqlitedatabase db = Oh.getreadabledatabase (); } //after the test framework has been initialized, this method calls before the test method executes.@Overrideprotected voidSetUp ()throwsException {Super. SetUp (); Oh=NewMyopenhelper (GetContext (), "people.db",NULL, 1); DB=oh.getwritabledatabase (); } //when the test method finishes executing, this method calls the@Overrideprotected voidTearDown ()throwsException {//TODO auto-generated Method Stub Super. TearDown (); Db.close (); } Public voidInsert () {//Db.execsql ("INSERT into person (name, salary, phone) values (?,?,?)", New object[]{"Little Chi's wife [1]", "13000", 138438 });//Db.execsql ("INSERT into person (name, salary, phone) values (?,?,?)", New object[]{"son of Xiao Zhi", 14000, "13888"}); /c5>Db.execsql ("INSERT into person (name, salary, phone) values (?,?,?)",Newobject[]{"Xiao Zhi", 14000, "13888"}); } Public voidDelete () {Db.execsql ("Delete from person where name =?",Newobject[]{, the "Little Chi"}); } Public voidUpdate () {Db.execsql ("Update person Set phone =?" WHERE name =? ",Newobject[]{186666, "Son of Xiao Zhi"}); } Public voidSelect () {cursor cursor= Db.rawquery ("Select name, Salary from",NULL); while(Cursor.movetonext ()) {//get the value of a column by column indexString name = cursor.getstring (Cursor.getcolumnindex ("name")); String Salary= cursor.getstring (1); SYSTEM.OUT.PRINTLN (Name+ ";" +salary); } } Public voidInsertapi () {//encapsulates all data to be inserted into the Contentvalues objectContentvalues values =Newcontentvalues (); Values.put ("Name", "Tour Tianlong"); Values.put ("Phone", "15999"); Values.put ("Salary", 16000); Db.insert ("Person",NULL, values); } Public voidDeleteapi () {inti = db.delete ("person", "name =?") and _id =? ",Newstring[]{"son of Xiao Zhi", "3"}); System.out.println (i); } Public voidUpdateapi () {contentvalues values=Newcontentvalues (); Values.put ("Salary", 26000); inti = db.update ("Person", values, "name =?",Newstring[]{"Tour Tianlong"}); System.out.println (i); } Public voidSelectapi () {cursor cursor= Db.query ("Person",NULL,NULL,NULL,NULL,NULL,NULL,NULL); while(Cursor.movetonext ()) {String name= Cursor.getstring (Cursor.getcolumnindex ("name")); String Phone= Cursor.getstring (Cursor.getcolumnindex ("Phone")); String Salary= Cursor.getstring (Cursor.getcolumnindex ("salary")); SYSTEM.OUT.PRINTLN (Name+ ";" + Phone + ";" +salary); } } Public voidtransaction () {Try{ //Open Transactiondb.begintransaction (); Contentvalues Values=Newcontentvalues (); Values.put ("Salary", 12000); Db.update ("Person", values, "name =?",Newstring[]{, the "Little Chi"}); Values.clear (); Values.put ("Salary", 16000); Db.update ("Person", values, "name =?",NewString[]{"The son of Xiao Zhi"}); inti = 3/0; //SET Transaction Execution Successdb.settransactionsuccessful (); } finally{ //Close the transaction, commit at the same time, if the transaction execution has been set successfully, then the SQL statement will take effect, and conversely, the SQL statement rollbackdb.endtransaction (); } }}
SQLite Database Learning