SQLite数据库工具类

SQLite数据库工具类,第1张

概述概述 该数据库工具类依赖了Room的相关包,使用很方便,现整理归档如下。使用时可以直接当做工具类使用。步骤 引入依赖 implementation 'android.arch.persistence:db:1.0.0'implementation 'android.arch.persistence:db-framework:1.0.0' 创建SupportDbManager package co 概述

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

总结

以上是内存溢出为你收集整理的SQLite数据库工具类全部内容,希望文章能够帮你解决SQLite数据库工具类所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1161967.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存