Android 資料儲存知識梳理(1) SQLiteOpenHelper 原始碼解析

澤毛發表於2017-12-21

一、概述

這篇文章主要涉及到專案當中,使用資料庫相關的操作:

  • 使用SQLiteOpenHelper來封裝資料庫。
  • 多執行緒的情況下使用SQLiteOpenHelper

二、使用SQLiteOpenHelper封裝資料庫

2.1 使用SQLiteOpenHelper的原因

之所以需要使用SQLiteOpenHelper,而不是呼叫Context的方法來直接得到SQLiteDatabase,主要是因為它有兩個好處:

  • 自動管理建立:當需要對資料庫進行操作的時候,不用關心SQLiteOpenHelper所關聯的SQLiteDatabase是否建立,SQLiteOpenHelper會幫我們去判斷,如果沒有建立,那麼就先建立該資料庫後,再返回給使用者。
  • 自動管理版本:當需要對資料庫進行操作之前,如果發現當前宣告的資料庫的版本和手機內的資料庫版本不同的時候,那麼會分別呼叫onUpgradeonDowngrade,這樣使用者就可以在裡面來處理新舊版本的相容問題。

2.2 SQLiteOpenHelperAPI

SQLiteOpenHelperAPI很少,我們來看一下:

Android 資料儲存知識梳理(1)   SQLiteOpenHelper 原始碼解析

2.2.1 建構函式

    /**
     * Create a helper object to create, open, and/or manage a database.
     * The database is not actually created or opened until one of
     * {@link #getWritableDatabase} or {@link #getReadableDatabase} is called.
     *
     * <p>Accepts input param: a concrete instance of {@link DatabaseErrorHandler} to be
     * used to handle corruption when sqlite reports database corruption.</p>
     *
     * @param context to use to open or create the database
     * @param name of the database file, or null for an in-memory database
     * @param factory to use for creating cursor objects, or null for the default
     * @param version number of the database (starting at 1); if the database is older,
     *     {@link #onUpgrade} will be used to upgrade the database; if the database is
     *     newer, {@link #onDowngrade} will be used to downgrade the database
     * @param errorHandler the {@link DatabaseErrorHandler} to be used when sqlite reports database
     * corruption, or null to use the default error handler.
     */
    public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version,
            DatabaseErrorHandler errorHandler) {
        if (version < 1) throw new IllegalArgumentException("Version must be >= 1, was " + version);

        mContext = context;
        mName = name;
        mFactory = factory;
        mNewVersion = version;
        mErrorHandler = errorHandler;
    }
複製程式碼

這裡有一點很重要:當我們例項化一個SQLiteOpenHelper的子類時,並不會立刻建立或者開啟它對應的資料庫,這個操作是等到呼叫了getWritableDatabase或者getReadableDatabase才進行的

  • context:用來開啟或者關閉資料的上下文,需要注意記憶體洩露問題。
  • name:資料庫的名字,一般為xxx.db,如果為空,那麼使用的是記憶體資料庫。
  • factory:建立cursor的工廠類,如果為空,那麼使用預設的。
  • version:資料庫的當前版本號,必須大於等於1
  • erroeHandler:資料庫發生錯誤時的處理者,如果為空,那麼使用預設處理方式。

2.2.2 獲得SQLiteDatabase

一般情況下,當我們例項完一個SQLiteOpenHelper物件之後,就可以通過它所關聯的SQLiteDatabase,來對資料庫進行操作了,獲得資料庫的方式有下面兩種:

    /**
     * Create and/or open a database that will be used for reading and writing.
     * The first time this is called, the database will be opened and
     * {@link #onCreate}, {@link #onUpgrade} and/or {@link #onOpen} will be
     * called.
     *
     * <p>Once opened successfully, the database is cached, so you can
     * call this method every time you need to write to the database.
     * (Make sure to call {@link #close} when you no longer need the database.)
     * Errors such as bad permissions or a full disk may cause this method
     * to fail, but future attempts may succeed if the problem is fixed.</p>
     *
     * <p class="caution">Database upgrade may take a long time, you
     * should not call this method from the application main thread, including
     * from {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
     *
     * @throws SQLiteException if the database cannot be opened for writing
     * @return a read/write database object valid until {@link #close} is called
     */
    public SQLiteDatabase getWritableDatabase() {
        synchronized (this) {
            return getDatabaseLocked(true);
        }
    }

    /**
     * Create and/or open a database.  This will be the same object returned by
     * {@link #getWritableDatabase} unless some problem, such as a full disk,
     * requires the database to be opened read-only.  In that case, a read-only
     * database object will be returned.  If the problem is fixed, a future call
     * to {@link #getWritableDatabase} may succeed, in which case the read-only
     * database object will be closed and the read/write object will be returned
     * in the future.
     *
     * <p class="caution">Like {@link #getWritableDatabase}, this method may
     * take a long time to return, so you should not call it from the
     * application main thread, including from
     * {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
     *
     * @throws SQLiteException if the database cannot be opened
     * @return a database object valid until {@link #getWritableDatabase}
     *     or {@link #close} is called.
     */
    public SQLiteDatabase c() {
        synchronized (this) {
            return getDatabaseLocked(false);
        }
    }
複製程式碼

注意到,它們最終都是呼叫了同一個方法,並且在該方法上加上了同步程式碼塊。

關於getWritableDatabase,原始碼當中提到了以下幾點:

  • 該方法是用來建立或者開啟一個可讀寫的資料庫,當這個方法第一次被呼叫時,資料庫會被開啟,並且onCreateonUpgrade或者onOpen方法可能會被呼叫。
  • 一旦開啟成功之後,這個資料庫會被快取,也就是其中的mDatabase成員變數,但是如果許可權檢查失敗或者磁碟慢了,那麼有可能會開啟失敗。
  • Upgrade方法有時候可能會執行耗時的操作,因此不要在主執行緒當中呼叫這個方法,包括ContentProvideronCreate()方法

關於getWritableDatabase,有幾點說明:

  • 在除了某些特殊情況,它和getWritableDatabase返回的一樣,都是一個可讀寫的資料庫,如果磁碟滿了,那麼才有可能返回一個只讀的資料庫。
  • 如果當前mDatabase是隻讀的,但是之後又呼叫了一個getWritableDatabase方法並且成功地獲取到了可寫的資料庫,那麼原來的mDatabase會被關閉,重新開啟一個可讀寫的資料庫,呼叫db.reopenReadWrite()方法。

下面,我們來看一下getDatabaseLocked的具體實現,來了解其中的細節問題:

    private SQLiteDatabase getDatabaseLocked(boolean writable) {
        if (mDatabase != null) {
            if (!mDatabase.isOpen()) {
                //如果使用者獲取了db物件,但不是通過SQLiteOpenHelper關閉它,那麼下次呼叫的時候會返回null。
                mDatabase = null;
            } else if (!writable || !mDatabase.isReadOnly()) {
                //如果不要求可寫或者當前快取的資料庫已經是可寫的了,那麼直接返回.
                return mDatabase;
            }
        }

        if (mIsInitializing) {
            throw new IllegalStateException("getDatabase called recursively");
        }

        SQLiteDatabase db = mDatabase;
        try {
            mIsInitializing = true;
            //如果要求可寫,但是當前快取的是隻讀的,那麼嘗試關閉後再重新開啟來獲取一個可寫的。
            if (db != null) {
                if (writable && db.isReadOnly()) {
                    db.reopenReadWrite();
                }
            //下面就是沒有快取的情況.
            } else if (mName == null) {
                db = SQLiteDatabase.create(null);
            //這裡就是我們第一次呼叫時候的情況.
            } else {
                try {
                    if (DEBUG_STRICT_READONLY && !writable) {
                        final String path = mContext.getDatabasePath(mName).getPath();
                        db = SQLiteDatabase.openDatabase(path, mFactory,
                                SQLiteDatabase.OPEN_READONLY, mErrorHandler);
                    } else {
                        //以可寫的方式開啟或者建立一個資料庫,注意這裡有一個標誌位mEnableWriteAheadLogging,我們後面來解釋.
                        db = mContext.openOrCreateDatabase(mName, mEnableWriteAheadLogging ?
                                Context.MODE_ENABLE_WRITE_AHEAD_LOGGING : 0,
                                mFactory, mErrorHandler);
                    }
                } catch (SQLiteException ex) {
                    //如果發生異常,並且要求可寫的,那麼直接丟擲異常.
                    if (writable) {
                        throw ex;
                    }
                    Log.e(TAG, "Couldn't open " + mName
                            + " for writing (will try read-only):", ex);
                    //如果不要求可寫,那麼嘗試呼叫只讀的方式來開啟。
                    final String path = mContext.getDatabasePath(mName).getPath();
                    db = SQLiteDatabase.openDatabase(path, mFactory,
                            SQLiteDatabase.OPEN_READONLY, mErrorHandler);
                }
            }
            //抽象方法,子類實現。
            onConfigure(db);
           
            final int version = db.getVersion();
            //如果新舊版本不想等,那麼才會進入下面的判斷.
            if (version != mNewVersion) {
                //當前資料庫是隻讀的,那麼會丟擲異常。
                if (db.isReadOnly()) {
                    throw new SQLiteException("Can't upgrade read-only database from version " +
                            db.getVersion() + " to " + mNewVersion + ": " + mName);
                }
                //開啟事務,onCreate/onDowngrade/OnUpgrade只會呼叫其中一個。
                db.beginTransaction();
                try {
                    if (version == 0) {
                        onCreate(db);
                    } else {
                        if (version > mNewVersion) {
                            onDowngrade(db, version, mNewVersion);
                        } else {
                            onUpgrade(db, version, mNewVersion);
                        }
                    }
                    db.setVersion(mNewVersion);
                    db.setTransactionSuccessful();
                } finally {
                    db.endTransaction();
                }
            }
            //資料庫開啟完畢.
            onOpen(db);

            if (db.isReadOnly()) {
                Log.w(TAG, "Opened " + mName + " in read-only mode");
            }

            mDatabase = db;
            return db;
        } finally {
            mIsInitializing = false;
            if (db != null && db != mDatabase) {
                db.close();
            }
        }
    }
複製程式碼

2.2.3 onConfig/onOpen

在上面獲取資料庫的過程中,有兩個方法:

    /**
     * Called when the database connection is being configured, to enable features
     * such as write-ahead logging or foreign key support.
     * <p>
     * This method is called before {@link #onCreate}, {@link #onUpgrade},
     * {@link #onDowngrade}, or {@link #onOpen} are called.  It should not modify
     * the database except to configure the database connection as required.
     * </p><p>
     * This method should only call methods that configure the parameters of the
     * database connection, such as {@link SQLiteDatabase#enableWriteAheadLogging}
     * {@link SQLiteDatabase#setForeignKeyConstraintsEnabled},
     * {@link SQLiteDatabase#setLocale}, {@link SQLiteDatabase#setMaximumSize},
     * or executing PRAGMA statements.
     * </p>
     *
     * @param db The database.
     */
    public void onConfigure(SQLiteDatabase db) {}
    /**
     * Called when the database has been opened.  The implementation
     * should check {@link SQLiteDatabase#isReadOnly} before updating the
     * database.
     * <p>
     * This method is called after the database connection has been configured
     * and after the database schema has been created, upgraded or downgraded as necessary.
     * If the database connection must be configured in some way before the schema
     * is created, upgraded, or downgraded, do it in {@link #onConfigure} instead.
     * </p>
     *
     * @param db The database.
     */
    public void onOpen(SQLiteDatabase db) {}
複製程式碼
  • onConfigure:在onCreate/onUpgrade/onDowngrade呼叫之前,可以在它其中來配置資料庫連線的引數,這時候資料庫已經建立完成,但是表有可能還沒建立,或者不是最新的。
  • onOpen:在資料庫連線配置完成,並且資料庫表已經更新到最新的,當我們在這裡對資料庫進行操作時,需要判斷它是否是隻讀的。

2.2.4 onCreate/onUpgrade/onDowngrade

    /**
     * Called when the database is created for the first time. This is where the
     * creation of tables and the initial population of the tables should happen.
     *
     * @param db The database.
     */
    public abstract void onCreate(SQLiteDatabase db);

    /**
     * Called when the database needs to be upgraded. The implementation
     * should use this method to drop tables, add tables, or do anything else it
     * needs to upgrade to the new schema version.
     *
     * <p>
     * The SQLite ALTER TABLE documentation can be found
     * <a href="http://sqlite.org/lang_altertable.html">here</a>. If you add new columns
     * you can use ALTER TABLE to insert them into a live table. If you rename or remove columns
     * you can use ALTER TABLE to rename the old table, then create the new table and then
     * populate the new table with the contents of the old table.
     * </p><p>
     * This method executes within a transaction.  If an exception is thrown, all changes
     * will automatically be rolled back.
     * </p>
     *
     * @param db The database.
     * @param oldVersion The old database version.
     * @param newVersion The new database version.
     */
    public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);

    /**
     * Called when the database needs to be downgraded. This is strictly similar to
     * {@link #onUpgrade} method, but is called whenever current version is newer than requested one.
     * However, this method is not abstract, so it is not mandatory for a customer to
     * implement it. If not overridden, default implementation will reject downgrade and
     * throws SQLiteException
     *
     * <p>
     * This method executes within a transaction.  If an exception is thrown, all changes
     * will automatically be rolled back.
     * </p>
     *
     * @param db The database.
     * @param oldVersion The old database version.
     * @param newVersion The new database version.
     */
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        throw new SQLiteException("Can't downgrade database from version " +
                oldVersion + " to " + newVersion);
    }
複製程式碼
  • onCreate原有資料庫版本為0時呼叫,在裡面我們進行剪標操作;而onUpgrade/onDowngrade則在不相等時呼叫,在裡面我們對錶的欄位進行更改。
  • onDowngrade的預設實現是丟擲異常。
  • onUpgrade沒有預設實現。
  • 這三個操作都是放在事務當中,如果發生了錯誤,那麼會回滾。

2.2.5 關閉

    /**
     * Close any open database object.
     */
    public synchronized void close() {
        if (mIsInitializing) throw new IllegalStateException("Closed during initialization");

        if (mDatabase != null && mDatabase.isOpen()) {
            mDatabase.close();
            mDatabase = null;
        }
    }
複製程式碼

會關閉當前快取的資料庫,並把清空mDatabase快取,注意這個方法也被加上了物件鎖。

三、多執行緒情況下對SQLiterDBHelper的使用

  • 在多執行緒的情況下,每個執行緒對同一個SQLiterDBHelper例項進行操作,並不會產生影響,因為剛剛我們看到,在獲取和關閉資料庫的方法上,都加上了物件鎖,所以最終我們只是開啟了一條到資料庫上的連線,這時候就轉變為去討論SQLiteDatabase的增刪改查操作是否是執行緒安全的了。
  • 然而,如果每個執行緒獲取SQLiteDatabase時,不是用的同一個SQLiterDBHelper,那麼其實是開啟了多個連線,假如通過這多個連線同資料庫的操作是沒有同步的話,那麼就會出現問題。

下面,我們總結一下在多執行緒情況下,可能出現問題的幾種場景:

3.1 多執行緒情況下每個執行緒建立一個SQLiteOpenHelper,並且之前沒有建立過關聯的db

    /**
     * 多執行緒同時建立,每個執行緒持有一個SQLiteOpenHelper
     * @param view
     */
    public void multiOnCreate(View view) {
        int threadCount = 50;
        for (int i = 0; i < threadCount; i++) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    MultiThreadDBHelper dbHelper = new MultiThreadDBHelper(MainActivity.this);
                    SQLiteDatabase database = dbHelper.getWritableDatabase();
                    ContentValues contentValues = new ContentValues(1);
                    contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_KEY, "thread_id");
                    contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_VALUE, String.valueOf(Thread.currentThread().getId()));
                    database.insert(MultiThreadDBContract.TABLE_KEY_VALUE.TABLE_NAME, null, contentValues);
                }
            };
            thread.start();
        }
    }
複製程式碼

在上面這種情況下,由於多個執行緒的getWritableDatabase沒有進行同步操作,並且這時候手機裡面沒有對應的資料庫,那麼就有可能出現下面的情況:

  • Thread#1呼叫getWritableDatabase,在其中獲取資料庫的版本號為0,因此它呼叫onCreate建表,建表完成。
  • Thread#1建表完成,但是還沒有來得及給資料庫設定版本號時,Thread#2也呼叫了getWritableDatabase,在其中它獲取資料庫版本號也是0,因此也執行了onCreate操作,那麼這時候就會出現對一個資料庫多次建立同一張表的情況發生。
    Android 資料儲存知識梳理(1)   SQLiteOpenHelper 原始碼解析

3.2 多執行緒情況下每個執行緒建立一個SQLiteOpenHelper,同時對關聯的db進行寫入操作

    /**
     * 多個執行緒同時寫入,每個執行緒持有一個SQLiteOpenHelper
     * @param view
     */
    public void multiWriteUseMultiDBHelper(View view) {
        MultiThreadDBHelper init = new MultiThreadDBHelper(MainActivity.this);
        SQLiteDatabase database = init.getWritableDatabase();
        database.close();
        int threadCount = 10;
        for (int i = 0; i < threadCount; i++) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    MultiThreadDBHelper dbHelper = new MultiThreadDBHelper(MainActivity.this);
                    SQLiteDatabase database = dbHelper.getWritableDatabase();
                    for (int i = 0; i < 1000; i++) {
                        ContentValues contentValues = new ContentValues(1);
                        contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_KEY, "thread_id");
                        contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_VALUE, String.valueOf(Thread.currentThread().getId()) + "_" + i);
                        database.insert(MultiThreadDBContract.TABLE_KEY_VALUE.TABLE_NAME, null, contentValues);
                    }
                }
            };
            thread.start();
        }
    }
複製程式碼

假如我們啟動了多個執行緒,並且在每個執行緒中新建了SQLiteOpenHelper例項,那麼當它們呼叫各自的getWritableDatabase方法時,其實是對手機中的db建立了多個資料庫連線,當通過多個資料庫連線同時對db進行寫入,那麼會丟擲下面的異常:

Android 資料儲存知識梳理(1)   SQLiteOpenHelper 原始碼解析
3.13.2我們就可以看出,在多執行緒的情況下,每個執行緒新建一個SQLiteOpenHelper會出現問題,因此,我們儘量把它設計為單例的模式,那麼是不是多個執行緒持有同一個SQLiteOpenHelper例項就不會出現問題呢,其實並不然,我們看一下下面這些共用同一個SQLiteOpenHelper的情形。

3.3 多執行緒情況下所有執行緒共用一個SQLiteOpenHelper,其中一個執行緒呼叫了close方法

    /**
     * 多執行緒下共用一個SQLiteOpenHelper
     * @param view
     */
    public void multiCloseUseOneDBHelper(View view) {
        final MultiThreadDBHelper init = new MultiThreadDBHelper(MainActivity.this);
        final SQLiteDatabase database = init.getWritableDatabase();
        database.close();
        Thread thread1 = new Thread() {

            @Override
            public void run() {
                SQLiteDatabase database = init.getWritableDatabase();
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    Log.e("MainActivity", "e=" + e);
                }
                ContentValues contentValues = new Conten;
                contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_KEY, "thread_id");
                contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_VALUE, String.valueOf(Thread.currentThread().getId()));
                //由於Thread2已經關閉了資料庫,因此這裡再呼叫插入操作就會出現問題。
                database.insert(MultiThreadDBContract.TABLE_KEY_VALUE.TABLE_NAME, null, contentValues);
            }
        };
        thread1.start();
        Thread thread2 = new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    Log.e("MainActivity", "e=" + e);
                }
                init.close();
            }
        };
        thread2.start();
    }
複製程式碼

Android 資料儲存知識梳理(1)   SQLiteOpenHelper 原始碼解析

3.4 多執行緒情況下所有執行緒共用一個SQLiteOpenHelper,在寫的過程中同時讀

由於是共用了同一個SQLiteOpenHelper,因此我們需要考慮的是對於同一個SQLiteDatabase連線,是否允許讀寫併發,預設情況下是不允許的,但是,我們可以通過SQLiteOpenHelper#setWriteAheadLoggingEnabled,這個配置預設是關的,當開啟時表示:它允許一個寫執行緒與多個讀執行緒同時在一個SQLiteDatabase上起作用。實現原理是寫操作其實是在一個單獨的檔案,不是原資料庫檔案。所以寫在執行時,不會影響讀操作,讀操作讀的是原資料檔案,是寫操作開始之前的內容。在寫操作執行成功後,會把修改合併會原資料庫檔案。此時讀操作才能讀到修改後的內容。但是這樣將花費更多的記憶體。

四、解決多執行緒的例子

工廠類負責根據dbName建立對應的SQLiteOpenHelper

public abstract class DBHelperFactory {
    public abstract SQLiteOpenHelper createDBHelper(String dbName);
}
複製程式碼

通過管理類來插入指定資料庫指定表

public class DBHelperManager {

    private HashMap<String, SQLiteOpenHelperWrapper> mDBHelperWrappers;
    private DBHelperFactory mDBHelperFactory;

    static class Nested {
        public static DBHelperManager sInstance = new DBHelperManager();
    }

    public static DBHelperManager getInstance() {
        return Nested.sInstance;
    }

    private DBHelperManager() {
        mDBHelperWrappers = new HashMap<>();
    }

    public void setDBHelperFactory(DBHelperFactory dbHelperFactory) {
        mDBHelperFactory = dbHelperFactory;
    }

    private synchronized SQLiteOpenHelperWrapper getSQLiteDBHelperWrapper(String dbName) {
        SQLiteOpenHelperWrapper wrapper = mDBHelperWrappers.get(dbName);
        if (wrapper == null) {
            if (mDBHelperFactory != null) {
                SQLiteOpenHelper dbHelper = mDBHelperFactory.createDBHelper(dbName);
                if (dbHelper != null) {
                    SQLiteOpenHelperWrapper newWrapper = new SQLiteOpenHelperWrapper();
                    newWrapper.mSQLiteOpenHelper = dbHelper;
                    newWrapper.mSQLiteOpenHelper.setWriteAheadLoggingEnabled(true);
                    mDBHelperWrappers.put(dbName, newWrapper);
                    wrapper = newWrapper;
                }
            }
        }
        return wrapper;
    }

    private synchronized SQLiteDatabase getReadableDatabase(String dbName) {
        SQLiteOpenHelperWrapper wrapper = getSQLiteDBHelperWrapper(dbName);
        if (wrapper != null && wrapper.mSQLiteOpenHelper != null) {
            return wrapper.mSQLiteOpenHelper.getReadableDatabase();
        } else {
            return null;
        }
    }

    private synchronized SQLiteDatabase getWritableDatabase(String dbName) {
        SQLiteOpenHelperWrapper wrapper = getSQLiteDBHelperWrapper(dbName);
        if (wrapper != null && wrapper.mSQLiteOpenHelper != null) {
            return wrapper.mSQLiteOpenHelper.getWritableDatabase();
        } else {
            return null;
        }
    }

    private class SQLiteOpenHelperWrapper {
        public SQLiteOpenHelper mSQLiteOpenHelper;
    }

    public long insert(String dbName, String tableName, String nullColumn, ContentValues contentValues) {
        SQLiteDatabase db = getWritableDatabase(dbName);
        if (db != null) {
            return db.insert(tableName, nullColumn, contentValues);
        }
        return -1;
    }

    public Cursor query(String dbName, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
        SQLiteDatabase db = getReadableDatabase(dbName);
        if (db != null) {
            return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
        }
        return null;
    }

    public int update(String dbName, String table, ContentValues values, String whereClause, String[] whereArgs) {
        SQLiteDatabase db = getWritableDatabase(dbName);
        if (db != null) {
            return db.update(table, values, whereClause, whereArgs);
        }
        return 0;
    }

    public int delete(String dbName, String table, String whereClause, String[] whereArgs) {
        SQLiteDatabase db = getWritableDatabase(dbName);
        if (db != null) {
            return db.delete(table, whereClause, whereArgs);
        }
        return 0;
    }

}
複製程式碼

多執行緒插入的方式改為下面這樣:

    public void multiWriteUseManager(View view) {
        int threadCount = 10;
        for (int i = 0; i < threadCount; i++) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    for (int i = 0; i < 1000; i++) {
                        ContentValues contentValues = new ContentValues(1);
                        contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_KEY, "thread_id");
                        contentValues.put(MultiThreadDBContract.TABLE_KEY_VALUE.COLUMN_VALUE, String.valueOf(Thread.currentThread().getId()) + "_" + i);
                        DBHelperManager.getInstance().insert(MultiThreadDBContract.DATABASE_NAME, MultiThreadDBContract.TABLE_KEY_VALUE.TABLE_NAME, null, contentValues);
                    }
                }
            };
            thread.start();
        }
    }
複製程式碼

五、小結

這篇文章主要介紹的是SQLiteOpenHelper,需要注意以下三點:

  • 不要在多執行緒情況且沒有進行執行緒同步的情況下,操作由不同的SQLiteOpenHelper物件所返回的SQLiteDatabase
  • 在多執行緒共用一個SQLiteOpenHelper時,需要注意關閉時,是否有其它執行緒正在使用該Helper所關聯的db
  • 在多執行緒共用一個SQLiteOpenHelper時,是否有同時讀寫的需求,如果有,那麼需要設定setWriteAheadLoggingEnabled標誌位。

對於SQLiteDatabase,還有更多的優化操作,當我們有關資料庫的錯誤時,我們都可以根據錯誤碼,在下面的網站當中找到說明:

https://www.sqlite.org/rescode.html

相關文章