這篇文章主要涉及到項目當中,使用數據庫相關的操做:html
SQLiteOpenHelper
來封裝數據庫。SQLiteOpenHelper
。SQLiteOpenHelper
封裝數據庫SQLiteOpenHelper
的緣由之因此須要使用SQLiteOpenHelper
,而不是調用Context
的方法來直接獲得SQLiteDatabase
,主要是由於它有兩個好處:android
SQLiteOpenHelper
所關聯的SQLiteDatabase
是否建立,SQLiteOpenHelper
會幫咱們去判斷,若是沒有建立,那麼就先建立該數據庫後,再返回給使用者。onUpgrade
和onDowngrade
,這樣使用者就能夠在裏面來處理新舊版本的兼容問題。SQLiteOpenHelper
的API
SQLiteOpenHelper
的API
不多,咱們來看一下: sql
/**
* 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
:數據庫發生錯誤時的處理者,若是爲空,那麼使用默認處理方式。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
,源碼當中提到了如下幾點:bash
onCreate
,onUpgrade
或者onOpen
方法可能會被調用。mDatabase
成員變量,可是若是權限檢查失敗或者磁盤慢了,那麼有可能會打開失敗。Upgrade
方法有時候可能會執行耗時的操做,所以不要在主線程當中調用這個方法,包括ContentProvider
的onCreate()
方法。關於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();
}
}
}
複製代碼
onConfig/onOpen
在上面獲取數據庫的過程當中,有兩個方法:app
/**
* 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
:在數據庫鏈接配置完成,而且數據庫表已經更新到最新的,當咱們在這裏對數據庫進行操做時,須要判斷它是不是隻讀的。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
沒有默認實現。/**
* 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
,那麼實際上是打開了多個鏈接,假如經過這多個鏈接同數據庫的操做是沒有同步的話,那麼就會出現問題。下面,咱們總結一下在多線程狀況下,可能出現問題的幾種場景:
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
操做,那麼這時候就會出現對一個數據庫屢次創建同一張表的狀況發生。
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
進行寫入,那麼會拋出下面的異常:
3.1
和
3.2
咱們就能夠看出,在多線程的狀況下,每一個線程新建一個
SQLiteOpenHelper
會出現問題,所以,咱們儘可能把它設計爲單例的模式,那麼是否是多個線程持有同一個
SQLiteOpenHelper
實例就不會出現問題呢,其實並否則,咱們看一下下面這些共用同一個
SQLiteOpenHelper
的情形。
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();
}
複製代碼
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
,還有更多的優化操做,當咱們有關數據庫的錯誤時,咱們均可以根據錯誤碼,在下面的網站當中找到說明: