標籤:
SQLite,是一款輕型的資料庫,是遵守ACID的關係型資料庫管理系統,它包含在一個相對小的C庫中。它是D.RichardHipp建立的公有領域項目。它的設計目標是嵌入式的,而且目前已經在很多嵌入式產品中使用了它,它佔用資源非常的低,在嵌入式裝置中,可能只需要幾百K的記憶體就夠了。它能夠支援Windows/Linux/Unix等等主流的作業系統,同時能夠跟很多程式語言相結合,比如 Tcl、C#、PHP、Java等,還有ODBC介面,同樣比起Mysql、PostgreSQL這兩款開源的世界著名資料庫管理系統來講,它的處理速度比他們都快。SQLite第一個Alpha版本誕生於2000年5月。 至2015年已經有15個年頭,SQLite也迎來了一個版本 SQLite 3已經發布。
下面簡單介紹其在安卓中的應用:
1.首先自己寫一個類MyOpenHelper繼承SQLiteOpenHelper,SQLiteOpenHelper是抽象類別,需要重寫構造方法和實現兩個方法。如下:
package com.example.sqlite;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 { /** * 重寫構造方法,因為父類沒有無參構造方法 * @param context * @param name * @param factory * @param version */ public MyOpenHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } /** * 資料庫建立時,此方法會調用 */ @Override public void onCreate(SQLiteDatabase db) { System.out.println("資料庫被建立了"); db.execSQL("create table person(_id integer primary key autoincrement," + " name varchar(10), salary varchar(20), phone integer(20))"); } /** * 資料庫升級時,此方法被調用 */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { System.out.println("資料庫升級了"); }}
2.在測試類別中進行測試,增刪改查,下面示範第一種,自己寫SQL語句:
package com.example.sqlite.test;import com.example.sqlite.MyOpenHelper;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.test.AndroidTestCase;public class TestCase extends AndroidTestCase { private MyOpenHelper oh; private SQLiteDatabase db; /** * 測試建立資料庫 * 即使運行兩遍,也只會建立一次資料庫 */ public void test1(){ MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 1); //如果資料庫不存在,先建立資料庫,再擷取可讀可寫的資料庫物件,如果資料庫存在,就直接開啟 SQLiteDatabase db = oh.getWritableDatabase(); //返回的和上面的沒區別,除非內部儲存空間滿了,返回唯讀資料庫 //SQLiteDatabase db = oh.getReadableDatabase(); } /** * 測試架構初始完畢之後,在測試方法執行之前,此方法調用 */ @Override protected void setUp() throws Exception { super.setUp(); oh = new MyOpenHelper(getContext(), "people.db", null, 1); db = oh.getWritableDatabase(); } /** * 測試方法執行之後,此方法調用 */ @Override protected void tearDown() throws Exception { super.tearDown(); db.close(); } /** * 測試插入 */ public void test2(){ db.execSQL("insert into person(name, salary, phone) values(?,?,?)", new Object[]{"陳馳","15000",151762}); } /** * 測試刪除 */ public void test3(){ db.execSQL("delete from person where name=?", new Object[]{"陳馳"}); } /** * 測試更新 */ public void test4(){ db.execSQL("update person set phone=? where name=?",new Object[]{123456, "陳馳"}); } /** * 測試查詢 */ public void test5(){ //注意,API變了,第二個參數是sql語句中?的填充 Cursor cursor = db.rawQuery("select name, salary from person", null); while(cursor.moveToNext()){ //通過列索引擷取列的值 String name = cursor.getString(cursor.getColumnIndex("name")); String salary = cursor.getString(cursor.getColumnIndex("salary")); System.out.println(name+",,,"+salary); } } }
3.在測試類別中進行測試,增刪改查,下面示範第二種,調用API:
public void insertApi(){ //把要插入的資料全部封裝至ContentValues對象 ContentValues values = new ContentValues(); values.put("name", "陳掣"); values.put("salary", "16000"); values.put("phone", 15176290645L); //返回的是最新插入的主索引值(表名,填寫null就行<只有在values為空白才有用>,插入的內容) long id = db.insert("person", null, values); System.out.println(id); } public void deleteApi(){ //返回的是刪除的行數(表名,條件,條件中?的填充) int rows = db.delete("person", "name=? and _id=?", new String[]{"陳馳", "10"}); System.out.println(rows); } public void updateApi(){ ContentValues values = new ContentValues(); values.put("salary", "777777"); //返回的是受影響的行數(表名,更改的內容,條件,條件中?的填充) int rows = db.update("person", values, "_id=?", new String[]{"7"}); System.out.println(rows); } public void selectApi(){ //(表名,查詢欄位<null為所有欄位>,查詢條件,查詢條件中?的填充,groupby,having,orderby,limit) Cursor cursor = db.query("person", null, "name=?", new String[]{"陳掣"}, null, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); String salary = cursor.getString(cursor.getColumnIndex("salary")); String phone = cursor.getString(cursor.getColumnIndex("phone")); System.out.println(name+","+salary+","+phone); } }
以上就是簡單的增刪改查操作,可以下載一個SQLite Expert視覺化檢視進行查看。
4.交易管理,類比一個轉賬的例子
public void transaction(){ try { //開啟事務 db.beginTransaction(); ContentValues values = new ContentValues(); values.put("salary", "19000"); db.update("person", values, "_id=?", new String[]{"11"}); values.clear(); values.put("salary", "13000"); db.update("person", values, "_id=?", new String[]{"12"}); //int i = 2/0; //如果在這裡拋異常,則設定事務執行成功的代碼不會執行,sql語句復原,update不生效 //設定事務執行成功 db.setTransactionSuccessful(); } catch (Exception e) { e.printStackTrace(); } finally { //關閉事務,同時提交,如果已經設定事務執行成功,那麼sql語句就生效了,反之,sql語句復原 db.endTransaction(); } }
SQLite資料庫的基本操作