Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

使用 SQLiteOpenHelper 類別進行 SQLite 資料庫的增修查詢

一、為什麼要自訂 Helper

操作 SQLite 資料庫時,需要先取得一個 SQLiteOpenHelper 物件。但它是個抽象類別,因此必須自訂一個類別來繼承它,並實作其中所有抽象方法。這個自訂類別就是後續所有資料庫操作的入口。

二、建立並繼承 SQLiteOpenHelper

建構式中記得呼叫基底類別的建構式,同時提供多組重載方便使用:

public class SqlLiteHelper extends SQLiteOpenHelper {

    private static final int VERSION = 1;

    // 實現基底類別的四參數建構式
    public SqlLiteHelper(Context context, String DBname, CursorFactory factory, int version)
    {
        super(context, DBname, factory, version);
    }

    // 實現基底類別的三參數建構式
    public SqlLiteHelper(Context context, String DBname, int version) {
        this(context, DBname, null, version);
    }

    // 實現基底類別的兩參數建構式
    public SqlLiteHelper(Context context, String DBname) {
        this(context, DBname, null, VERSION);
    }

    // 當建立資料庫時執行
    @Override
    public void onCreate(SQLiteDatabase db) {
        System.out.println("建立資料庫");
        db.execSQL("Create table user(id int, name varchar(20))");
    }

    // 當更新資料庫版本時執行
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        System.out.println("修改資料庫版本號");
    }
}

三、建立資料庫

只有呼叫 getWritableDatabase()(或 getReadableDatabase())時,onCreate 才會真正被觸發:

// 建立自訂 SqlLiteHelper 的實例,呼叫兩參數建構式
SqlLiteHelper mySqlLiteHelper = new SqlLiteHelper(SqlLiteTestActivity.this, "paj_db_test");
// 只有呼叫 getWritableDatabase() 才會執行 onCreate 中的程式碼
mySqlLiteHelper.getWritableDatabase();

四、更新資料庫版本

版本號只能遞增。當版本變高時,onUpgrade 會被呼叫:

// 呼叫三參數建構式更新版本
SqlLiteHelper myhelper = new SqlLiteHelper(getApplicationContext(), "paj_db_test", 2);
myhelper.getReadableDatabase();

五、插入資料

// 建立 Helper 物件
SqlLiteHelper myhelper = new SqlLiteHelper(getApplicationContext(), "paj_db_test");

// 建立 ContentValues 物件存放資料
ContentValues contentValues = new ContentValues();
// 鍵與值對應資料庫中的欄位與值,值必須與欄位型別相符
contentValues.put("id", 2);
contentValues.put("name", "zhangsan");

// 取得可寫的 SQLiteDatabase 物件
SQLiteDatabase db = myhelper.getWritableDatabase();
// 呼叫 insert:第一個參數為表名,第二個傳 null,第三個為 ContentValues
db.insert("user", null, contentValues);

六、更新資料

// 建立 ContentValues 存放要更新的內容
ContentValues contentValues = new ContentValues();
contentValues.put("name", "zhangsanfeng");

SqlLiteHelper myhelper = new SqlLiteHelper(getApplicationContext(), "paj_db_test");
SQLiteDatabase db = myhelper.getWritableDatabase();

// update 的四個參數依序是:
// 1. 資料表名稱
// 2. ContentValues 物件
// 3. WHERE 條件子句,其中的 ? 對應第四參數的字串陣列
// 4. WHERE 的條件值,以字串陣列存放,有幾個問號就放幾個元素
db.update("user", contentValues, "id=?", new String[]{"1"});

使用 id=? 搭配參數陣列,而不是把值直接拼進 SQL 字串,是避免 SQL Injection 的標準做法,值得養成習慣。

七、查詢資料

SqlLiteHelper myhelper = new SqlLiteHelper(SqlLiteTestActivity.this, "paj_db_test");
// 取得唯讀的 SQLiteDatabase 物件
SQLiteDatabase db = myhelper.getReadableDatabase();

// query 的參數依序為:
// 1. 表名
// 2. 欄位名稱陣列,傳 null 代表全部
// 3. WHERE 條件子句,? 對應第四參數
// 4. WHERE 條件值
// 5. group by 分組
// 6. having
// 7. order by 排序
Cursor cursor = db.query("user", null, null, null, null, null, null);

// moveToNext() 把游標下移一列;游標預設停在第一列的上一列
while (cursor.moveToNext()) {
    // getColumnIndex 取得欄位序號,再交給 getString 取值
    String nameString = cursor.getString(cursor.getColumnIndex("name"));
    String idsString  = cursor.getString(cursor.getColumnIndex("id"));
    System.out.println("ID--->" + idsString);
    System.out.println("NAME--->" + nameString);
}

最後提醒兩點:查詢完畢記得呼叫 cursor.close(),避免游標洩漏;而在背景執行緒進行資料庫讀寫、完成後再回主執行緒更新畫面,才是符合 Android 規範的做法。

Copyright © Linux教程網 All Rights Reserved