# How to set password in android

**URL:** <https://discuss.zetetic.net/t/how-to-set-password-in-android/6204>\
**Category:** Issues\
**Created:** [January 9, 2024, 10:12pm UTC](https://discuss.zetetic.net/t/how-to-set-password-in-android/6204 "2024-01-09T22:12:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Dexter](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/paul_dexter/32/2130_2.png) [@Paul\_Dexter](https://discuss.zetetic.net/u/Paul_Dexter)\
**Post date:** [January 9, 2024, 10:12pm UTC](https://discuss.zetetic.net/t/how-to-set-password-in-android/6204/1 "2024-01-09T22:12:12Z")

</div>

My old code is

```auto
    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

```auto
    @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

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [January 10, 2024, 2:00pm UTC](https://discuss.zetetic.net/t/how-to-set-password-in-android/6204/2 "2024-01-10T14:00:30Z")

</div>

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](https://github.com/sqlcipher/sqlcipher-android/blob/master/sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteOpenHelper.java#L173) 

2. [sqlcipher-android/sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteOpenHelper.java at master · sqlcipher/sqlcipher-android · GitHub](https://github.com/sqlcipher/sqlcipher-android/blob/master/sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteOpenHelper.java#L206) 

3. [sqlcipher-android/sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteDatabase.java at master · sqlcipher/sqlcipher-android · GitHub](https://github.com/sqlcipher/sqlcipher-android/blob/master/sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteDatabase.java)

---

<div class="post-metadata">

**Author:** ![Paul\_Dexter](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/paul_dexter/32/2130_2.png) [@Paul\_Dexter](https://discuss.zetetic.net/u/Paul_Dexter)\
**Post date:** [January 10, 2024, 4:08pm UTC](https://discuss.zetetic.net/t/how-to-set-password-in-android/6204/3 "2024-01-10T16:08:51Z")

</div>

Old code was

```auto
 super(context, DB_NAME, null, DATABASE_VERSION);

```

I updated it to

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

```

All seems to work, thanks for your help.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [January 10, 2024, 5:24pm UTC](https://discuss.zetetic.net/t/how-to-set-password-in-android/6204/4 "2024-01-10T17:24:02Z")

</div>

Hi @Paul_Dexter,

Great, glad to hear that resolved the issue!
