SQLite数据库工具类

前端之家收集整理的这篇文章主要介绍了SQLite数据库工具类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

数据库工具类依赖了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下载

https://download.csdn.net/download/jdfkldjlkjdl/10363666

原文链接:https://www.f2er.com/sqlite/198006.html

猜你在找的Sqlite相关文章