Android Development Learning-SQLite database and unit test, androidsqlite

Source: Internet
Author: User
Tags name database

Android Development Learning-SQLite database and unit test, androidsqlite

SQLite Database
Lightweight relational databases
The api required to create a database: SQLiteOpenHelper

 

Public class Myopenhelper extends SQLiteOpenHelper {// public Myopenhelper (Context context, String name, CursorFactory factory, int version) {// name database file name, cursor factory, database version super (context, name, factory, version);} // called at database creation @ Override public void onCreate (SQLiteDatabase db ){
// Create a table when creating a database. You can add, delete, modify, and query the table here. You only need to change the SQL statement in it !!! Db.exe cSQL ("create table person (_ id integer primary key autoincrement, name char (10), phone char (20), money integer (20 ))");} // this method is called during Database Upgrade @ Override public void onUpgrade (SQLiteDatabase arg0, int arg1, int arg2 ){}}

Create a database

Public class Testcase extends AndroidTestCase {public void test () {// getContext (): Obtain the virtual context. This is a test framework that facilitates the test of Myopenhelper oh = new Myopenhelper (getContext (), "people. db ", null, 1); // if the database does not exist, create a database first, and then obtain the readable and writable database objects. If yes, open SQLiteDatabase db = oh. getWritableDatabase (); // It is also a readable and writable database object. The database must be opened in read-only mode unless the storage space is full. // SQLiteDatabase db1 = oh. getReadableDatabase ();}}

The result is as follows:

Add, delete, modify, and query Databases
SQL statement
* Insert into person (name, phone, money) values ('zhang san', '123', 159874611 );
* Delete from person where name = 'Li si' and _ id = 4;
* Update person set money = 6000 where name = 'Li si ';
* Select name, phone from person where name = 'zhang san ';

Execute SQL statements to add, delete, modify, and query

// Insert
Db.exe cSQL ("insert into person (name, phone, money) values (?, ?, ?); ", New Object [] {" James ", 15987461,750 00 });
// Search
Cursor cs = db. rawQuery ("select _ id, name, money from person where name = ?; ", New String [] {" James "});
This method is called before the test method is executed.

Protected void setUp () throws Exception {
Super. setUp ();
// Obtain the virtual context object
Oh = new MyOpenHelper (getContext (), "people. db", null, 1 );
}
Use APIs for addition, deletion, modification, and query
* Insert
// Save the data to be stored in the database as a key-Value Pair
ContentValues cv = new ContentValues ();
Cv. put ("name", "Liu Neng ");
Cv. put ("phone", 1651646 );
Cv. put ("money", 3500 );
// The returned value is the primary key of the modified row. If an error occurs,-1 is returned.
Long I = db. insert ("person", null, cv );
* Delete

// The returned value is the number of deleted rows.
Int I = db. delete ("person", "_ id =? And name =? ", New String [] {" 1 "," James "});
* Modify

ContentValues cv = new ContentValues ();
Cv. put ("money", 25000 );
Int I = db. update ("person", cv, "name =? ", New String [] {" Zhao Si "});
* Query
// Arg1: The field to be queried
// Arg2: Query Condition
// Arg3: a placeholder for filling in query Conditions
Cursor cs = db. query ("person", new String [] {"name", "money"}, "name =? ", New String [] {" zhangsan "}, null );
While (cs. moveToNext ()){
// Obtain the index value of the specified Column
String name = cs. getString (cs. getColumnIndex ("name "));
String money = cs. getString (cs. getColumnIndex ("money "));
System. out. println (name + ";" + money );
}

 

Unit Test junit
Define a class that inherits AndroidTestCase. Define a method in the class to test this method.

 

Add:
When specifying the command set, targetPackage specifies the package name of the application you want to test.
<Instrumentation
Android: name = "android. test. InstrumentationTestRunner"
Android: targetPackage = "com. itheima. junit" // press alt +/to select it!
> </Instrumentation>
Define the class library used
<Uses-library android: name = "android. test. runner"> </uses-library>



The role of assertion to check whether the running result is consistent with the expectation
If an application exception occurs, it is thrown to the test framework.

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.