SQLite資料庫工具類
阿新 • • 發佈:2019-02-11
概述
該資料庫工具類依賴了Room的相關包,使用很方便,現整理歸檔如下。使用時可以直接當做工具類使用。
步驟引入依賴
implementation 'android.arch.persistence:db:1.0.0'
implementation 'android.arch.persistence:db-framework:1.0.0'
建立SupportDbManager
package com.example.a002034.myapplication.db; import android.arch.persistence.db.SupportSQLiteDatabase; import android.arch.persistence.db.SupportSQLiteOpenHelper; import android.support.annotation.NonNull; import java.util.concurrent.atomic.AtomicInteger; /** * DbManager. * * @author xuzhuyun */ public class SupportDbManager { private SupportSQLiteOpenHelper mOpenHelper; private SupportSQLiteDatabase mDb; private AtomicInteger mOpenCounter; public SupportDbManager(@NonNull SupportSQLiteOpenHelper openHelper) { this.mOpenHelper = openHelper; this.mOpenCounter = new AtomicInteger(); } public void destroy() { this.mOpenHelper.close(); this.mOpenHelper = null; this.mDb = null; this.mOpenCounter.set(0); this.mOpenCounter = null; } public synchronized SupportSQLiteDatabase openDb() { if (this.mOpenCounter.incrementAndGet() == 1) { this.mDb = this.mOpenHelper.getWritableDatabase(); } return this.mDb; } public synchronized void closeDb() { if (this.mOpenCounter.decrementAndGet() == 0) { this.mOpenHelper.close(); } } }
建立DbHelper
package com.example.a002034.myapplication.db; /** * . * * @author */ import android.arch.persistence.db.SupportSQLiteDatabase; import android.arch.persistence.db.SupportSQLiteOpenHelper; import android.arch.persistence.db.framework.FrameworkSQLiteOpenHelperFactory; import android.util.Log; import com.example.a002034.myapplication.Configs; import com.example.a002034.myapplication.RootApp; /** * SQLiteOpenHelper. * * @author xuzhuyun */ public class DbHelper { private static final String TAG = "DbHelper"; private SupportSQLiteOpenHelper mOpenHelper; public DbHelper() { SupportSQLiteOpenHelper.Configuration configuration = SupportSQLiteOpenHelper.Configuration.builder(RootApp.getInstance()) .name(Configs.DB_NAME) .callback(new DbCallback(Configs.DB_VERSION)) .build(); SupportSQLiteOpenHelper.Factory factory = new FrameworkSQLiteOpenHelperFactory(); mOpenHelper = factory.create(configuration); } public SupportSQLiteOpenHelper getOpenHelper() { return mOpenHelper; } class DbCallback extends SupportSQLiteOpenHelper.Callback { DbCallback(int version) { super(version); } @Override public void onCreate(SupportSQLiteDatabase db) { Log.i(TAG, "[db] 建立資料庫"); // db.execSQL(Cabinet.CREATE_TABLE); // db.execSQL(Cell.CREATE_TABLE); // db.execSQL(Parcel.CREATE_TABLE); } @Override public void onUpgrade(SupportSQLiteDatabase db, int oldVersion, int newVersion) { Log.i(TAG, "[db] 升級資料庫,db = [" + db.getPath() + "], oldVersion = [" + oldVersion + "], currentVersion = [" + newVersion + "]"); } } }
以上RootApp是我們自定義的Application類,目的是獲取Context物件,這裡自己獲取就好.我的RootApp程式碼是:
package com.example.a002034.myapplication; import android.app.Application; /** * Application. * * @author xuzhuyun */ public class RootApp extends Application { private static final String TAG = "RootApp"; private static RootApp mContext; @Override public void onCreate() { super.onCreate(); mContext = this; //初始化資料庫 Configs.getInstance().init(); } public static RootApp getInstance() { if (mContext == null) { mContext = new RootApp(); } return mContext; } }
記得在Application類中初始化資料庫。
建立用到的配置類Configs
package com.example.a002034.myapplication;
import com.example.a002034.myapplication.db.DbHelper;
import com.example.a002034.myapplication.db.SupportDbManager;
/**
* 全域性配置類.
*
* @author xuzhuyun
*/
public class Configs {
/**
* 資料庫名稱.
*/
public static final String DB_NAME = "test.db";
/**
* 資料庫版本號.
*/
public static final int DB_VERSION = 1;
private SupportDbManager mDbManager;
private Configs() {
}
public void init() {
initPersistent();
}
public SupportDbManager getDbManager() {
if (mDbManager == null) {
throw new IllegalStateException("must invoke method init() first");
}
return mDbManager;
}
private void initPersistent() {
DbHelper smartDbHelper = new DbHelper();
mDbManager = new SupportDbManager(smartDbHelper.getOpenHelper());
}
private static class SingletonHolder {
private static final Configs INSTANCE = new Configs();
}
public static Configs getInstance() {
return SingletonHolder.INSTANCE;
}
}
呼叫
獲取SupportDbManager物件和SupportSQLiteDatabase物件
SupportDbManager dbManager = Configs.getInstance().getDbManager();
SupportSQLiteDatabase db = dbManager.openDb();
拿到這些物件後,我們就可以做資料庫增刪改查操作了。其中,結合該工具類,我們還可以整合sqldelight外掛,以後再補上sqldelight的用法。