Android. sqlcipher_export - unable to open database


#1

Hi everyone. I have a problem when trying to export my database. I have exeption:
net.sqlcipher.database.SQLiteException: unable to open database

My code:

String dbPath = context.getDatabasePath(DATABASE_NAME).getPath();

        File dbPathFile = new File(dbPath);
        if (!dbPathFile.exists())
            dbPathFile.getParentFile().mkdirs();

        SQLiteDatabase database = SQLiteDatabase.openDatabase(dbPath, password, null, 0);
        database.rawExecSQL("PRAGMA key = '" + password + "'");
        database.rawExecSQL("ATTACH DATABASE '" + DATABASE_NAME + "' AS SQLChiperSample KEY ''");
        database.rawExecSQL("SELECT sqlcipher_export('SQLChiperSample')");
        database.rawExecSQL("DETACH DATABASE SQLChiperSample");

The error appears on this line:

 database.rawExecSQL("ATTACH DATABASE '" + DATABASE_NAME + "' AS SQLChiperSample KEY ''");

What’s the error?


#2

Hello @AlexTereschuk

There are a couple of items here that need to be adjusted. First, you do not need to perform a PRAGMA key …; operation as that is handled for you in the line above when you provide the password to the database connection. Next, you already have a connection to DATABASE_NAME open, referenced by the database variable, if you want to attach another database, you would need to use an alternative name to attach to for performing your export. As an example, please take a look at example #2 in the documentation here.