@Override public File getAttachmentDirectory(Context context, String id) { // we store attachments in the database directory return context.getDatabasePath(id + ".db_att"); }
@Override public File getDatabase(Context context, String id) { return context.getDatabasePath(id + ".db"); }
public class DbBackupHelper extends FileBackupHelper { public DbBackupHelper(Context ctx, String dbName) { super(ctx, ctx.getDatabasePath(dbName).getAbsolutePath()); } }
@Override public File getDatabasePath(String name) { return mBase.getDatabasePath(name); }
private static boolean doesDatabaseExist(Context context, String dbName) { File dbFile = context.getDatabasePath(dbName); return dbFile.exists(); }
@Override public List<File> getDatabaseFiles() { List<File> databaseFiles = new ArrayList<File>(); for (String databaseName : mContext.databaseList()) { databaseFiles.add(mContext.getDatabasePath(databaseName)); } return databaseFiles; } }
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; }
/** * 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); }
/** * 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); } }
/** * 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); }
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(); }
@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)); }
private SQLiteDatabase createDatabase(String filename) { databasePath = ApplicationProvider.getApplicationContext().getDatabasePath(filename); databasePath.getParentFile().mkdirs(); return SQLiteDatabase.openOrCreateDatabase(databasePath.getPath(), null); }
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(); } }
/** * 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; } }
private SQLiteDatabase openOrCreateDatabase(String name) { return openOrCreateDatabase(ApplicationProvider.getApplicationContext().getDatabasePath(name)); }
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)); }
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); }
@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(); }
@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); }