一个app含有多个数据库注意点

(1)数据库的升级不是主动触发的(不是新的apk安装就自动升级数据库)而是在代码初始化数据库的时候,如果你的app包含多个数据库的话,那么就会单独在初始化自己的数据库的时候才会进行自己的数据库初始化操作。


(2)数据库在最开始创建的时候如果使用的不是数据加密的话,那么后面再对数据库加密会直接崩溃掉,一般报如下的错:
net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master;

那么你就要删除掉apk包,重新再来。在第一次创建数据库的时候使用加密。


(3)我经过测试,多个已经加密的数据库,可以单独进行数据库升级。


(4)多个数据库在初始化当前数据库的,最好是关闭之前打开的数据库。如下:

public class DbManager {

    private ClassEntityDao mClassEntityDao;
    private StudentEntityDao mStudentEntityDao;
    private Database mDb;

    private DbManager() {

    }

    private static final class Holder {
        private static final DbManager INSTANCE = new DbManager();
    }

    public static DbManager getInstance() {
        return Holder.INSTANCE;
    }

    public void init(Context context, String dbName) {
        release();
        CusOpenHelper helper = new CusOpenHelper(context, dbName);
        mDb = helper.getEncryptedWritableDb("yedashen");
        DaoMaster daoMaster = new DaoMaster(mDb);
        DaoSession daoSession = daoMaster.newSession();
        mClassEntityDao = daoSession.getClassEntityDao();
        mStudentEntityDao = daoSession.getStudentEntityDao();
    }

    public ClassEntityDao getClassEntityDao() {
        return mClassEntityDao;
    }

    public StudentEntityDao getStudentEntityDao() {
        return mStudentEntityDao;
    }

    public void release() {
        if (mDb != null) {
            mDb.close();
        }
    }
}
在初始化之前记得close其他的数据库。

猜你喜欢

转载自blog.csdn.net/qq_34723470/article/details/79668471
今日推荐