private static synchronized DbUtils getInstance(DaoConfig daoConfig) { DbUtils dao = daoMap.get(daoConfig.getDbName()); if (dao == null) { dao = new DbUtils(daoConfig); daoMap.put(daoConfig.getDbName(), dao); } else { dao.daoConfig = daoConfig; } // update the database if needed SQLiteDatabase database = dao.database; int oldVersion = database.getVersion(); int newVersion = daoConfig.getDbVersion(); if (oldVersion != newVersion) { if (oldVersion != 0) { DbUpgradeListener upgradeListener = daoConfig.getDbUpgradeListener(); if (upgradeListener != null) { upgradeListener.onUpgrade(dao, oldVersion, newVersion); } else { try { dao.dropDb(); } catch (DbException e) { LogUtils.e(e.getMessage(), e); } } } database.setVersion(newVersion); } return dao; }
private FinalDb(DaoConfig config) { if (config == null) throw new DbException("daoConfig is null"); if (config.getContext() == null) throw new DbException("android context is null"); if (config.type == 1) { File file = new File(config.getContext().getFilesDir(), config.getDbName()); if (!file.exists()) { // 拷贝数据库 try { InputStream is = config.getContext().getAssets().open(config.getDbName()); Utils.copyFile(is, file); } catch (Exception e) { e.printStackTrace(); } } this.db = SQLiteDatabase.openOrCreateDatabase(file, null); } else if (config.getTargetDirectory() != null && config.getTargetDirectory().trim().length() > 0 && config.type != 1) { this.db = createDbFileOnSDCard(config.getTargetDirectory(), config.getDbName()); } else { this.db = new SqliteDbHelper( config.getContext().getApplicationContext(), config.getDbName(), config.getDbVersion(), config.getDbUpdateListener()) .getWritableDatabase(); } this.config = config; }
private static synchronized FinalDb getInstance(DaoConfig daoConfig) { FinalDb dao = daoMap.get(daoConfig.getDbName()); if (dao == null) { dao = new FinalDb(daoConfig); daoMap.put(daoConfig.getDbName(), dao); } return dao; }
private SQLiteDatabase createDatabase(DaoConfig config) { SQLiteDatabase result = null; String dbDir = config.getDbDir(); if (!TextUtils.isEmpty(dbDir)) { File dir = new File(dbDir); if (dir.exists() || dir.mkdirs()) { File dbFile = new File(dbDir, config.getDbName()); result = SQLiteDatabase.openOrCreateDatabase(dbFile, null); } } else { result = config.getContext().openOrCreateDatabase(config.getDbName(), 0, null); } return result; }