android.content.Context.getDatabasePath()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(925)

本文整理了Java中android.content.Context.getDatabasePath()方法的一些代码示例,展示了Context.getDatabasePath()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.getDatabasePath()方法的具体详情如下:
包路径:android.content.Context
类名称:Context
方法名:getDatabasePath

Context.getDatabasePath介绍

暂无

代码示例

代码示例来源:origin: k9mail/k-9

@Override
public File getAttachmentDirectory(Context context, String id) {
  // we store attachments in the database directory
  return context.getDatabasePath(id + ".db_att");
}

代码示例来源:origin: k9mail/k-9

@Override
public File getDatabase(Context context, String id) {
  return context.getDatabasePath(id + ".db");
}

代码示例来源:origin: stackoverflow.com

public class DbBackupHelper extends FileBackupHelper {

  public DbBackupHelper(Context ctx, String dbName) {
    super(ctx, ctx.getDatabasePath(dbName).getAbsolutePath());
  }
}

代码示例来源:origin: oasisfeng/condom

@Override public File getDatabasePath(String name) {
  return mBase.getDatabasePath(name);
}

代码示例来源:origin: stackoverflow.com

private static boolean doesDatabaseExist(Context context, String dbName) {
  File dbFile = context.getDatabasePath(dbName);
  return dbFile.exists();
}

代码示例来源:origin: facebook/stetho

@Override
 public List<File> getDatabaseFiles() {
  List<File> databaseFiles = new ArrayList<File>();
  for (String databaseName : mContext.databaseList()) {
   databaseFiles.add(mContext.getDatabasePath(databaseName));
  }
  return databaseFiles;
 }
}

代码示例来源:origin: amitshekhariitbhu/Android-Debug-Database

public static HashMap<String, Pair<File, String>> getDatabaseFiles(Context context) {
  HashMap<String, Pair<File, String>> databaseFiles = new HashMap<>();
  try {
    for (String databaseName : context.databaseList()) {
      String password = getDbPasswordFromStringResources(context, databaseName);
      databaseFiles.put(databaseName, new Pair<>(context.getDatabasePath(databaseName), password));
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  return databaseFiles;
}

代码示例来源:origin: andpor/react-native-sqlite-storage

/**
 * Delete a database.
 *
 * @param dbname   The name of the database file
 *
 * @return true if successful or false if an exception was encountered
 */
@SuppressLint("NewApi")
private boolean deleteDatabaseNow(String dbname) {
  File dbfile = this.getContext().getDatabasePath(dbname);
  return android.database.sqlite.SQLiteDatabase.deleteDatabase(dbfile);
}

代码示例来源:origin: cymcsg/UltimateAndroid

/**
   * Open a database which is used for both reading and writing.
   * If the database is not exists,the method will create a database from the assets{@link #copyDatabase(android.content.Context, java.io.File, String)}.
   * @param context
   * @param databaseName
   * @return
   */
  public synchronized SQLiteDatabase getWritableDatabase(Context context,String databaseName) {
    File dbFile = context.getDatabasePath(databaseName);
    if (dbFile != null && !dbFile.exists()) {
      try {
        copyDatabase(context,dbFile,databaseName);
      } catch (IOException e) {
        throw new RuntimeException("Copying database error", e);
      }
    }

    return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READWRITE);
  }
}

代码示例来源:origin: cymcsg/UltimateAndroid

/**
 * Open a database which is used for reading only.
 * If the database is not exists,the method will create a database from the assets{@link #copyDatabase(android.content.Context, java.io.File, String)}.
 *
 * @param context
 * @param databaseName
 * @return
 * @throws SQLiteException
 */
public synchronized SQLiteDatabase getReadableDatabase(Context context,String databaseName) throws SQLiteException{
  File dbFile = context.getDatabasePath(databaseName);
  if (dbFile != null && !dbFile.exists()) {
    try {
      copyDatabase(context,dbFile,databaseName);
    } catch (IOException e) {
      throw new RuntimeException("Copying database error", e);
    }
  }
  return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
}

代码示例来源:origin: seven332/EhViewer

public static void initialize(Context context) {
  sHasOldDB = context.getDatabasePath("data").exists();
  DBOpenHelper helper = new DBOpenHelper(
      context.getApplicationContext(), "eh.db", null);
  SQLiteDatabase db = helper.getWritableDatabase();
  DaoMaster daoMaster = new DaoMaster(db);
  sDaoSession = daoMaster.newSession();
}

代码示例来源:origin: robolectric/robolectric

@Test
public void getDatabasePath_shouldAllowAbsolutePaths() throws Exception {
  String testDbName;
  if (System.getProperty("os.name").startsWith("Windows")) {
   testDbName = "C:\\absolute\\full\\path\\to\\db\\abc.db";
  } else {
   testDbName = "/absolute/full/path/to/db/abc.db";
  }
  File dbFile = context.getDatabasePath(testDbName);
  assertThat(dbFile).isEqualTo(new File(testDbName));
}

代码示例来源:origin: robolectric/robolectric

private SQLiteDatabase createDatabase(String filename) {
 databasePath = ApplicationProvider.getApplicationContext().getDatabasePath(filename);
 databasePath.getParentFile().mkdirs();
 return SQLiteDatabase.openOrCreateDatabase(databasePath.getPath(), null);
}

代码示例来源:origin: commonsguy/cw-omnibus

static void encrypt(Context ctxt) {
 SQLiteDatabase.loadLibs(ctxt);
 File dbFile=ctxt.getDatabasePath(DATABASE_NAME);
 File legacyFile=ctxt.getDatabasePath(LEGACY_DATABASE_NAME);
 if (!dbFile.exists() && legacyFile.exists()) {
  SQLiteDatabase db=
    SQLiteDatabase.openOrCreateDatabase(legacyFile, "", null);
  db.rawExecSQL(String.format("ATTACH DATABASE '%s' AS encrypted KEY '%s';",
                dbFile.getAbsolutePath(), PASSPHRASE));
  db.rawExecSQL("SELECT sqlcipher_export('encrypted')");
  db.rawExecSQL("DETACH DATABASE encrypted;");
  int version=db.getVersion();
  db.close();
  db=SQLiteDatabase.openOrCreateDatabase(dbFile, PASSPHRASE, null);
  db.setVersion(version);
  db.close();
  legacyFile.delete();
 }
}

代码示例来源:origin: andpor/react-native-sqlite-storage

/**
 * Delete a database.
 *
 * @param dbname   The name of the database file
 *
 * @return true if successful or false if an exception was encountered
 */
private boolean deleteDatabaseNow(String dbname) {
  File dbfile = this.getContext().getDatabasePath(dbname);
  try {
    return this.getContext().deleteDatabase(dbfile.getAbsolutePath());
  } catch (Exception ex) {
    FLog.e(TAG, "couldn't delete database", ex);
    return false;
  }
}

代码示例来源:origin: robolectric/robolectric

private SQLiteDatabase openOrCreateDatabase(String name) {
return openOrCreateDatabase(ApplicationProvider.getApplicationContext().getDatabasePath(name));
}

代码示例来源:origin: greenrobot/greenDAO

public void testFileisEncrypted() throws IOException {
  EncryptedDbUtils.assertEncryptedDbUsed(db);
  SimpleEntity simpleEntity = createEntityWithRandomPk();
  String text = "Catch me if you can";
  simpleEntity.setSimpleString(text);
  dao.insert(simpleEntity);
  File dbFile = getContext().getDatabasePath(ENCRYPTED_DB_FILE);
  int length = (int) dbFile.length();
  assertTrue(length > 0);
  byte[] buffer = new byte[length];
  // TODO readAll
  int read = new FileInputStream(dbFile).read(buffer);
  String contents = new String(buffer, 0, read, "US-ASCII");
  assertFalse(contents, contents.startsWith("SQLite"));
  assertFalse(contents, contents.contains("CREATE TABLE"));
  assertFalse(contents, contents.contains(text));
}

代码示例来源:origin: greenrobot/greenDAO

public static Database createDatabase(Context context, String dbName, String password) {
  if (!loadedLibs) {
    loadedLibs = true;
    SQLiteDatabase.loadLibs(context);
  }
  SQLiteDatabase sqLiteDatabase;
  if (dbName == null) {
    sqLiteDatabase = SQLiteDatabase.create(null, password);
  } else {
    File dbFile = context.getDatabasePath(dbName);
    dbFile.getParentFile().mkdir();
    context.deleteDatabase(dbName);
    sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(dbFile, password, null);
  }
  return new EncryptedDatabase(sqLiteDatabase);
}

代码示例来源:origin: robolectric/robolectric

@Before
public void setUp() throws Exception {
 final File databasePath = ApplicationProvider.getApplicationContext().getDatabasePath("path");
 databasePath.getParentFile().mkdirs();
 database = SQLiteDatabase.openOrCreateDatabase(databasePath.getPath(), null);
 SQLiteStatement createStatement = database.compileStatement("CREATE TABLE `routine` (`id` INTEGER PRIMARY KEY AUTOINCREMENT , `name` VARCHAR , `lastUsed` INTEGER DEFAULT 0 ,  UNIQUE (`name`)) ;");
 createStatement.execute();
 SQLiteStatement createStatement2 = database.compileStatement("CREATE TABLE `countme` (`id` INTEGER PRIMARY KEY AUTOINCREMENT , `name` VARCHAR , `lastUsed` INTEGER DEFAULT 0 ,  UNIQUE (`name`)) ;");
 createStatement2.execute();
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testGetPath() throws Exception {
 final String path1 = "path1", path2 = "path2";
 TestOpenHelper helper1 =
   new TestOpenHelper(ApplicationProvider.getApplicationContext(), path1, null, 1);
 String expectedPath1 =
   ApplicationProvider.getApplicationContext().getDatabasePath(path1).getAbsolutePath();
 assertThat(helper1.getReadableDatabase().getPath()).isEqualTo(expectedPath1);
 TestOpenHelper helper2 =
   new TestOpenHelper(ApplicationProvider.getApplicationContext(), path2, null, 1);
 String expectedPath2 =
   ApplicationProvider.getApplicationContext().getDatabasePath(path2).getAbsolutePath();
 assertThat(helper2.getReadableDatabase().getPath()).isEqualTo(expectedPath2);
}

相关文章

Context类方法