Android開發學習——SQLite資料庫與單元測試,androidsqlite

來源:互聯網
上載者:User

Android開發學習——SQLite資料庫與單元測試,androidsqlite

SQLite資料庫
 輕量級關係型資料庫
 建立資料庫需要使用的api:SQLiteOpenHelper

 

public class Myopenhelper extends SQLiteOpenHelper {    //new 時調用    public Myopenhelper(Context context, String name, CursorFactory factory,            int version) {        //name資料庫檔案名,遊標工廠,資料庫版本        super(context, name, factory, version);            }    //資料庫建立時調用    @Override    public void onCreate(SQLiteDatabase db) {
//在建立資料庫時建立表..這裡可以增刪改查,只要換裡面的sql語句就可以了!!! db.execSQL("create table person (_id integer primary key autoincrement, name char(10), phone char(20), money integer(20))"); } //資料庫升級時此方法調用 @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { }}

建立資料庫

public class Testcase extends AndroidTestCase {    public void test(){        //getContext():擷取虛擬上下文,這是測試架構特地方便測試        Myopenhelper oh = new Myopenhelper(getContext(), "people.db", null, 1);        //如果資料庫不存在,現先建立資料庫,再擷取可讀可寫的資料庫物件;若存在直接開啟        SQLiteDatabase db = oh.getWritableDatabase();        //也是可讀可寫的資料庫物件,除非儲存空間滿了,需要資料庫以唯讀方式 開啟。        //SQLiteDatabase db1  = oh.getReadableDatabase();            }}

結果如下:

 資料庫的增刪改查
SQL語句
* insert into person (name, phone, money) values ('張三', '159874611', 2000);
* delete from person where name = '李四' and _id = 4;
* update person set money = 6000 where name = '李四';
* select name, phone from person where name = '張三';

執行SQL語句實現增刪改查

        //插入
        db.execSQL("insert into person (name, phone, money) values (?, ?, ?);", new Object[]{"張三", 15987461, 75000});
        //尋找
        Cursor cs = db.rawQuery("select _id, name, money from person where name = ?;", new String[]{"張三"});
測試方法執行前會調用此方法

        protected void setUp() throws Exception {
            super.setUp();
            //                    擷取虛擬內容物件
            oh = new MyOpenHelper(getContext(), "people.db", null, 1);
        }
使用api實現增刪改查
* 插入
        //以索引值對的形式儲存要存入資料庫的資料
        ContentValues cv = new ContentValues();
        cv.put("name", "劉能");
        cv.put("phone", 1651646);
        cv.put("money", 3500);
        //傳回值是改行的主鍵,如果出錯返回-1
        long i = db.insert("person", null, cv);
* 刪除

        //傳回值是刪除的行數
        int i = db.delete("person", "_id = ? and name = ?", new String[]{"1", "張三"});
* 修改
    
        ContentValues cv = new ContentValues();
        cv.put("money", 25000);
        int i = db.update("person", cv, "name = ?", new String[]{"趙四"});
* 查詢
        //arg1:要查詢的欄位
        //arg2:查詢條件
        //arg3:填充查詢條件的預留位置
        Cursor cs = db.query("person", new String[]{"name", "money"}, "name = ?", new String[]{"張三"}, null, null, null);
        while(cs.moveToNext()){
            //                            擷取指定列的索引值
            String name = cs.getString(cs.getColumnIndex("name"));
            String money = cs.getString(cs.getColumnIndex("money"));
            System.out.println(name + ";" + money);
        }

 

單元測試junit
定義一個類繼承AndroidTestCase,在類中定義方法,即可測試該方法

 

在資訊清單檔裡加:
在指定指令集時,targetPackage指定你要測試的應用的包名
        <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.itheima.junit"    //這個按  alt+/ 自己選吧!
        ></instrumentation>
定義使用的類庫
 <uses-library android:name="android.test.runner"></uses-library>



 斷言的作用,檢測運行結果和預期是否一致
 如果應用出現異常,會拋給測試架構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.