Android_03 _ database operations

Source: Internet
Author: User

Android_03 _ database operations

MyOpenHelper. java

 

package com.itheima.sqlitedatabase;import java.sql.ResultSet;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class MyOpenHelper extends SQLiteOpenHelper {public MyOpenHelper(Context context, String name, CursorFactory factory,int version) {super(context, name, factory, version);// TODO Auto-generated constructor stub}
// This method is called when a database is created
// The value of version must be greater than or equal to, @ Overridepublic void onCreate (SQLiteDatabase db) mongodb.exe cSQL (create table person (_ id integer primary key autoincrement, name char (10 ), salary char (20), phone integer (20 )));}
// When the version value is greater, the database will be upgraded // when the database is upgraded, this method will call @ Overridepublic void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {System. out. println (Database upgraded );}}

TestCase. java

 

 

Package com. itheima. sqlitedatabase. test; import com. itheima. sqlitedatabase. myOpenHelper; import android. content. contentValues; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import android. test. androidTestCase; public class TestCase extends AndroidTestCase {// two methods are provided here: one is to manually write SQL statements, one is to call the system API // at this time, the test framework has not been initialized, and there is no virtual context object // private MyOpenHelper oh = new MyOpenHelper (GetContext (), people. db, null, 1); private MyOpenHelper oh; private SQLiteDatabase db; public void test () {// getContext (): obtain a virtual context MyOpenHelper oh = new MyOpenHelper (getContext (), people. db, null, 1); // if the database does not exist, first create a database and then obtain readable and writable database objects. If the database exists, open SQLiteDatabase db = oh. getWritableDatabase (); // If the storage space is full, the read-only database object is returned. // SQLiteDatabase db = oh. getReadableDatabase ();} // after the test framework is initialized, call this method before the test method is executed (this is the method in the test framework) @ O Verrideprotected void setUp () throws Exception {super. setUp (); oh = new MyOpenHelper (getContext (), people. db, null, 1); db = oh. getWritableDatabase ();} // after the test method of the test framework is executed, this method is called (this is the method in the test framework) @ Overrideprotected void tearDown () throws Exception {// TODO Auto-generated method stubsuper. tearDown (); db. close ();} public void insert () {// db.exe cSQL (insert into person (name, salary, phone) values (?, ?, ?), New Object [] {Xiaozhi's wife [1], 13000,138 438}); // db.exe cSQL (insert into person (name, salary, phone) values (?, ?, ?), New Object [] {son of Xiao Zhi, 14000,138 88w.mongodb.exe cSQL (insert into person (name, salary, phone) values (?, ?, ?), New Object [] {Xiaozhi, 14000,138 88});} public void delete(mongodb.exe cSQL (delete from person where name = ?, New Object [] {Xiaozhi});} public void update(mongomongodb.exe cSQL (update person set phone =? Where name = ?, New Object [] {186666, son of Xiaozhi});} public void select () {Cursor cursor = db. rawQuery (select name, salary from person, null); while (cursor. moveToNext () {// obtain the value of the column through the column index String name = cursor. getString (cursor. getColumnIndex (name); String salary = cursor. getString (1); System. out. println (name +; + salary) ;}} public void insertApi () {// encapsulate all the data to be inserted into the ContentValues object ContentValues values = new ContentValues (); values. put (Name, youtianlong); values. put (phone, 15999); values. put (salary, 16000); db. insert (person, null, values);} public void deleteApi () {int I = db. delete (person, name =? And _ id = ?, New String [] {son of Xiaozhi, 3}); System. out. println (I);} public void updateApi () {ContentValues values = new ContentValues (); values. put (salary, 26000); int I = db. update (person, values, name = ?, New String [] {youtianlong}); System. out. println (I);} public void selectApi () {Cursor cursor = db. query (person, 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 ++; + sala Ry) ;}} public void transaction () {// Add try-finally here to ensure that the transaction is successful or not, finally, you must ensure that the transaction closes try {// The role of the transaction is to ensure that the code between enabling and closing the transaction is either executed in full or rolled back, it is equivalent to not executing // starting the transaction db. beginTransaction (); ContentValues values = new ContentValues (); values. put (salary, 12000); db. update (person, values, name = ?, New String [] {Xiaozhi}); // clear () is used to clear all data values in values. clear (); values. put (salary, 16000); db. update (person, values, name = ?, New String [] {son of Xiaozhi}); // sets the db for successful transaction execution. setTransactionSuccessful () ;}finally {// close the transaction and commit at the same time. If the transaction has been set to run successfully, the SQL statement takes effect. Otherwise, the SQL statement rolls back the database. endTransaction ();}}}

 

Note: here we use the unit test framework for testing, so in the configuration file, remember to add two attributes:

Android: name = android. test. InstrumentationTestRunner
Android: targetPackage = com. itheima. sqlitedatabase
>

 

 

Note: Only an empty database is created in the MyOpenHelper. java file.

Add, delete, modify, and query databases in the test unit TestCase. java.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.