Test. java:
/*** The problem solved in this example: * core problem: Create a database object through the SQLiteOpenHelper class * perform operations on the database data through the database object * 1. operate the SQLite database using SQL statements * 2. operations on SQLite databases using APIs provided by Google * 3. SQLite operations on Transactions */import com. ghsy. createsqlitedb. db. myOpenHelper; import android. content. contentValues; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import android. test. androidTestCase; public class Test extends AndroidTestCase {MyOpenHelper oh; SQLiteDa Tabase db; public void test () {// create a MyOpenHelper object // modify the version number, A column MyOpenHelper oh = new MyOpenHelper (getContext (), "people. db ", null, 3); // if the database does not exist, first create a database and then open the database. if it already exists, directly open SQLiteDatabase db = oh. getWritableDatabase (); db. close ();} // test framework initialization completed/*** This method is called before a test is executed */@ Overrideprotected void setUp () throws Exception {// TODO Auto-generated me Thod stubsuper. setUp (); oh = new MyOpenHelper (getContext (), "people. db ", null, 3); db = oh. getWritableDatabase ();} // =============== operate the SQLite database using SQL statements =============================== ============= public void insert () mongodb.exe cSQL ("insert into person (name, phone, money) values (?, ?, ?) ", New Object [] {" Daming "," 18666 ", 6000 contains invalid mongodb.exe cSQL (" insert into person (name, phone, money) values (?, ?, ?) ", New Object [] {" Xiaohong "," 18666 ", 6000 inserting into mongodb.exe cSQL (" insert into person (name, phone, money) values (?, ?, ?) ", New Object [] {" "," 18666 ", 6000});} public void delete () mongodb.exe cSQL (" delete from person where name =? ", New Object [] {" Daming "});} public void update () mongodb.exe cSQL (" update person set money = 10000 where name =? ", New Object [] {" James "});} public void query () {// Cursor, which stores the data returned by the query. The method for obtaining the data is highly similar to that of resultSet. Cursor c = db. rawQuery ("select * from person", new String [] {}); while (c. moveToNext () {String name = c. getString (c. getColumnIndex ("name"); String phone = c. getString (c. getColumnIndex ("phone"); System. out. println (name + ";" + phone );}} /// =================== operations performed by APIs provided by Google on the SQLite database ==================== =======/ *** api-insert data Table */public void insertApi () {ContentValues cv = new ContentValues (); cv. put ("name", "weiming"); cv. put ("phone", 15666); cv. put ("money", 630); long id = db. insert ("person", null, cv); System. out. println (id);}/*** api-delete data from table ** @ return the number of rows affected */public int deleteApi () {int affectedNum = db. delete ("person", "name =? ", New String [] {" "}); return affectedNum;}/*** api-update */public void updateApi () {ContentValues contentValues = new ContentValues (); contentValues. put ("name", "Xiaohong"); contentValues. put ("money", "10"); // return the number of rows affecteddb. update ("person", contentValues, "name =? ", New String [] {" "});} public void queryApi () {Cursor cursor = db. query ("person", new String [] {"phone", "money"}, null, null); while (cursor. moveToNext () {String phone = cursor. getString (cursor. getColumnIndex ("phone"); String money = cursor. getString (cursor. getColumnIndex ("money"); System. out. println (phone + "#" + money );}} // ======================== SQLite operations on the transaction ========================== === /*** Bank transfer operation */public void transation () {db. beginTransaction (); try {// The user whose name is weiming transfers ContentValues contentValues = new ContentValues (); contentValues to Xiaohong. put ("money", 1000); db. update ("person", contentValues, "name =? ", New String [] {" weiming "}); ContentValues contentValues2 = new ContentValues (); contentValues2.put (" money ", 1100); db. update ("person", contentValues2, "name =? ", New String [] {" "}); // all statements have been executed. If no exception occurs, this statement is executed to set the mark db for successful transactions. setTransactionSuccessful () ;}finally {// The transaction identifier is checked. If the setTransactionSuccessful () flag is not called, the transaction is rolled back. Otherwise, the transaction is committed. Db. endTransaction ();}}}
MyOpenHelper. java
** SQLiteOpenHelper: * A helper class to manage database creation and version management. * therefore, SQLiteOpenHelper is used to operate the database itself. To operate data in the database, you must use the library object method. */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 {// name: database file name/factory: cursor factory/version: version, must be greater than or equal to 1 public MyOpenHelper (Context context, string name, CursorFactory factory, int version) {super (context, name, factory, version ); // TODO Auto-generated constructor stub} // called at database creation @ Overridepublic void onCreate (SQLiteDatabase db) {// create a personable table db.exe cSQL ("create table person (_ id integer primary key autoincrement, name char (10), phone char (20)"); System. out. println ("oncreate called");} // @ Overridepublic void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stubSystem. out. println ("onupgrade called" mongomongodb.exe cSQL ("alter table person add money char (20 )");}}