0

I had been using SQLite in my project and I have to migrate to SQLCipher. I have a constructor in my DatabaseHelper class. I don't create the db anywhere else. My constructor is:

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

I have added SQLiteDatabase.loadLibs(this); in onCreate() of the activity where the data is inserted for the first time.

Whenever I open my .sqlite file in the SQLite managers, it shows all the data. I am not able to understand where to put the code to create the database file as mentioned in docs.

Can someone please guide me?

The Bat
  • 1,085
  • 1
  • 13
  • 31
  • 1
    "I am not able to understand where to put the code to create the database file as mentioned in docs" -- um, that would be the same as how you use `SQLiteOpenHelper` for ordinary SQLite. In your code, where you have `getReadableDatabase()` or `getWriteableDatabase()`, if the database does not exist, it will be created. The SQLCipher for Android editions of those methods take the passphrase, and so you should aim to open the database shortly after collecting the passphrase. – CommonsWare May 23 '17 at 13:30
  • @CommonsWare how can I set the passphrase because I am not doing SQLiteDatabase.openOrCreateDatabase(databaseFile, "test123", null) this step anywhere – The Bat May 23 '17 at 13:33
  • 1
    Use your `DatabaseHelper`, assuming that it is inheriting from the SQLCipher for Android edition of `SQLiteOpenHelper`. As I noted in my previous comment, `getReadableDatabase()` and `getWriteableDatabase()` will take your passphrase. Otherwise, `SQLiteOpenHelper` works the same here as it does with ordinary SQLite. – CommonsWare May 23 '17 at 13:38
  • @CommonsWare I have used getWriteableDatabase("") and getReadableDatabase("") like this passing empty passphrase because I don't know what passphrase to pass. I haven't set any – The Bat May 23 '17 at 13:40
  • Collect a passphrase from the user. That is the point of having an encrypted database. So, have some UI where the user provides the passphrase, and that in turn allows you to supply that passphrase to SQLCipher for Android. – CommonsWare May 23 '17 at 13:43
  • @CommonsWare assume that I have collected that passphrase in a string, where do I set that String as the passphrase of my database? I am sorry I am unable to make myself clear I guess. – The Bat May 23 '17 at 13:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144939/discussion-between-prakhar-srivastava-and-commonsware). – The Bat May 23 '17 at 13:49
  • 1
    "where do I set that String as the passphrase of my database?" -- you get rid of the `""` and pass that `String` to `getReadableDatabase()` and/or `getWriteableDatabase()`. Those methods will create your database if it does not exist. That is covered in [the documentation for `SQLiteOpenHelper`](https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html). The only difference with SQLCipher for Android is supplying the passphrase to those methods, which is not needed with standard SQLite. – CommonsWare May 23 '17 at 13:50
  • @CommonsWare so if I do that, will it stop showing data in SQLite managers? – The Bat May 23 '17 at 13:56
  • 1
    I do not know what "SQLite managers" means. Some SQLite tools support SQLCipher; those would give you the option of providing the passphrase. SQLite tools that do not support SQLCipher would be unable to read the database. This is the point behind encryption. – CommonsWare May 23 '17 at 13:57
  • @CommonsWare okay.. I have user getWritableDatabase and getReadableDatabase at many parts of the code. I need to replace everywhere even to test if it is working? Or can I test by using the passphrase in one of the tables data entry/ – The Bat May 23 '17 at 14:04
  • 1
    "I have user getWritableDatabase and getReadableDatabase at many parts of the code" -- particularly with SQLCipher for Android, that is not a good plan. Open it once (typically with `getWritableDatabase()`) for the life of your process. – CommonsWare May 23 '17 at 14:07
  • @CommonsWare hey it worked.. Thank you so much.. for the help and also for dealing with my questions which might have been difficult to understand everytime .. :) – The Bat May 24 '17 at 11:41

0 Answers0