概述
该数据库工具类依赖了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的用法。
Demo下载