Why use SQLiteOpenHelper over SQLiteDatabase?

前端 未结 4 587
时光说笑
时光说笑 2020-12-29 18:48

In my activity I have for example

SQLiteDatabase db = openOrCreateDatabase(Preferences.DB_NAME, Context.MODE_PRIVATE, null);
db.execSQL(\"CREATE TABLE IF NOT         


        
4条回答
  •  一个人的身影
    2020-12-29 19:38

    Above of other answers, one very important feature in SQLiteOpenHelper class, it has 2 synchronized methods, getWritableDatabase() and getReadableDatabase().

    That means your database operations are thread safe.

    Code snippet from SQLiteOpenHelper class

    public SQLiteDatabase getReadableDatabase() {
        synchronized (this) {
            return getDatabaseLocked(false);
        }
    }
    

    and

    public SQLiteDatabase getWritableDatabase() {
        synchronized (this) {
            return getDatabaseLocked(true);
        }
    }
    

提交回复
热议问题