0

I am attempting to implement Sqlcipher for android. I am using this for multiple activities and as such am extending SQLiteOpenHelper {}.

However I am

I am aware that the OnCreate override method is not called until the database is accessed for the first time.

My oncreate method looks like

@Override
public void onCreate(SQLiteDatabase db) {
    SQLiteDatabase.loadLibs(context);


    File databaseFile = context.getDatabasePath("SyncableDiabetesDatabase.db");
    databaseFile.mkdirs();
    databaseFile.delete();
    mDataBase = SQLiteDatabase.openOrCreateDatabase(databaseFile,SuperKey, null);

I am attempting to get the OnCreate Method to call this method by calling this.getWritableDatabase(SuperKey); However when I run this command i receive the error

net.sqlcipher.database.SQLiteException: attempt to write a readonly database: PRAGMA user_version = 1

I'm not exactly sure how this is possible since the command is for a writeable DB

I know sqlcipher is correctly installed, as I can put the DB creation code above outside of OnCreate and the data is stored correctly. However the file is overridden each time the activity is called than, hence why I am moving it to OnCreate which is only called once when I create the tables.

Any insight here would be more than welcome.

I can provide any additional code snippits if needed.

Thank you

user1874538
  • 262
  • 2
  • 15
  • possible duplicate of [SQLiteReadOnlyDatabaseException: attempt to write a readonly database](http://stackoverflow.com/questions/9884969/sqlitereadonlydatabaseexception-attempt-to-write-a-readonly-database) – Simas Jul 12 '14 at 22:34
  • None of the code that you have shown belongs in `onCreate()` of a `SQLiteOpenHelper`. I **strongly** recommend that you put SQLCipher aside for a while and learn how to use SQLite first. You may wish to read [the documentation for how to use SQLite](http://developer.android.com/guide/topics/data/data-storage.html#db). – CommonsWare Jul 12 '14 at 22:35
  • Hi, I appreciate the comments, but I have extenive experience with SQLite, and have had it running on this application since the apps creation. I am attempting to migrate to SqlCipher just as additional security. If not in onCreate where would you recommend accessing the database file, as it must persist between app openings, and will be overridden if it is outside the constructor? And the readonly database link is interesting, however the current SqLite database has no issues with getWritable – user1874538 Jul 12 '14 at 22:41

1 Answers1

0

You do not need to call SQLiteDatabase.openOrCreateDatabase within the onCreate method of the SQLiteOpenHelper, the database instance is already provided as a parameter to the function. An example of using the SQLiteOpenHelper can be found here.

Nick Parker
  • 1,378
  • 1
  • 7
  • 10