コード例 #1
0
 /**
  * 获取缓存中的数据库
  *
  * @param dbfile assets里的数据库路径
  */
 public SQLiteDatabase getDatabase(String dbfile) {
   SQLiteDatabase db = databases.get(dbfile);
   if (db != null && db.isOpen()) {
     if (SQLiteDebug.isDebug) {
       Log.i(TAG, String.format("Return a database copy of %s", dbfile));
     }
     return (SQLiteDatabase) databases.get(dbfile);
   }
   return null;
 }
コード例 #2
0
 /** Close all assets database */
 public static void closeAllDatabase() {
   if (SQLiteDebug.isDebug) {
     Log.i(TAG, "closeAllDatabase");
   }
   if (mInstance != null) {
     Set<String> set = mInstance.databases.keySet();
     Iterator<String> iterator = set.iterator();
     while (iterator.hasNext()) {
       String key = iterator.next();
       SQLiteDatabase db = mInstance.databases.get(key);
       if (db.isOpen()) {
         db.close();
       }
       iterator.remove();
     }
   }
 }
コード例 #3
0
  /**
   * Get a assets database, if this database is opened this method is only return a copy of the
   * opened database
   *
   * @param dbfile db文件路径(在assets中的文件路径)
   * @param passwd 密码
   * @return, if success it return a SQLiteDatabase object else return null
   */
  public SQLiteDatabase getDatabase(String dbfile, String passwd) {
    SQLiteDatabase db = databases.get(dbfile);
    if (db != null && db.isOpen()) {
      if (SQLiteDebug.isDebug) {
        Log.i(TAG, String.format("Return a database copy of %s", dbfile));
      }
      return (SQLiteDatabase) databases.get(dbfile);
    }
    if (context == null) return null;
    if (SQLiteDebug.isDebug) {
      Log.i(TAG, String.format("Create database %s", dbfile));
    }
    String spath = databasepath;
    String sfile = getDatabaseFile(dbfile);

    File file = new File(sfile);
    SharedPreferences dbs = context.getSharedPreferences(AssetsDatabaseManager.class.toString(), 0);
    // Get Database file flag, if true means this database file was copied and valid
    boolean flag = dbs.getBoolean(dbfile, false);
    if (!flag || !file.exists()) {
      file = new File(spath);
      if (!file.exists() && !file.mkdirs()) {
        if (SQLiteDebug.isDebug) {
          Log.i(TAG, "Create \"" + spath + "\" fail!");
        }
        return null;
      }
      if (!copyAssetsToFilesystem(dbfile, sfile)) {
        if (SQLiteDebug.isDebug) {
          Log.i(TAG, String.format("Copy %s to %s fail!", dbfile, sfile));
        }
        return null;
      }

      dbs.edit().putBoolean(dbfile, true).commit();
    }

    db = SQLiteDatabase.openDatabase(sfile, passwd, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    if (db != null) {
      databases.put(dbfile, db);
    }
    return db;
  }