How to set password in android

My old code is

    public static DataBaseManager getInstance(Context context) {
        //  context = AppContext.getAppContext();
        mContext = context;

        if (sInstance == null && context != null) {
            sInstance = new DataBaseManager(context);
            mDataBase = sInstance.getWritableDatabase(masterpassword);
        }
        return sInstance;
    }

But since I have upgrade to

implementation 'net.zetetic:sqlcipher-android:4.5.5@aar'
implementation 'androidx.sqlite:sqlite:2.4.0'

getWritableDatabase doesn’t allow the password parameter.
I have tried adding the password in the oncreate

    @Override
    public void onCreate(SQLiteDatabase db) {
        System.loadLibrary("sqlcipher");
        try {
            db.execSQL("PRAGMA key = '"+masterpassword+"'");
            db.execSQL("CREATE TABLE IF NOT EXISTS..

But this still doesn’t work and if it did work this call is only used when creating the db so I’ve no idea how to set the password when calling queries.

Can someone point me in the right direction, this is a new application and not any sort of update

Thanks

1 Like

Hi @Paul_Dexter,

With sqlcipher-android, if you are using a SQLiteOpenHelper derivative, you can provide the password as a String [1], or byte [] [2] to the constructor. Alternatively, you can use one of the static methods [3] on the SQLiteDatabase class to open and/or create an encrypted database file.


  1. sqlcipher-android/sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteOpenHelper.java at master · sqlcipher/sqlcipher-android · GitHub ↩︎

  2. sqlcipher-android/sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteOpenHelper.java at master · sqlcipher/sqlcipher-android · GitHub ↩︎

  3. sqlcipher-android/sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteDatabase.java at master · sqlcipher/sqlcipher-android · GitHub ↩︎

1 Like

Old code was

 super(context, DB_NAME, null, DATABASE_VERSION);

I updated it to

super(context, DB_NAME, masterpassword, null, DATABASE_VERSION, 1, null, null, false);

All seems to work, thanks for your help.

Hi @Paul_Dexter,

Great, glad to hear that resolved the issue!