# Change Password of the Database

**URL:** <https://discuss.zetetic.net/t/change-password-of-the-database/509>\
**Category:** Issues\
**Created:** [April 9, 2015, 10:46am UTC](https://discuss.zetetic.net/t/change-password-of-the-database/509 "2015-04-09T10:46:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinay\_kumar](https://avatars.discourse-cdn.com/v4/letter/v/59ef9b/32.png) [@vinay\_kumar](https://discuss.zetetic.net/u/vinay_kumar)\
**Post date:** [April 9, 2015, 10:46am UTC](https://discuss.zetetic.net/t/change-password-of-the-database/509/1 "2015-04-09T10:46:17Z")

</div>

Hi All,

I see that there is a method in `net.sqlcipher.database.SQLiteOpenHelper` `changePassword(string)`. Is there any specific process to do this ? Meaning, I tried this method which ultimately threw

`file is encrypted or is not a database` when using onCreate(db) db object

or below error when using getWriteableDatabase(currentPassword)

```
net.sqlcipher.database.SQLiteException: unknown error
04-09 16:13:47.629: E/AndroidRuntime(23895): at net.sqlcipher.database.SQLiteDatabase.native_rekey(Native Method

```

)  
Is this method intended to literally change the password of database at runtime ? Pls clarify

---

<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:** [April 9, 2015, 12:45pm UTC](https://discuss.zetetic.net/t/change-password-of-the-database/509/2 "2015-04-09T12:45:04Z")

</div>

Hello @vinay_kumar

The `changePassword(…)` function performs a native call to `sqlite3_rekey`. Please note you must have successfully keyed the database prior to making this call. The documentation for the native call can be found [here](https://www.zetetic.net/sqlcipher/sqlcipher-api/#sqlite3_rekey).

---

<div class="post-metadata">

**Author:** ![vinay\_kumar](https://avatars.discourse-cdn.com/v4/letter/v/59ef9b/32.png) [@vinay\_kumar](https://discuss.zetetic.net/u/vinay_kumar)\
**Post date:** [April 10, 2015, 3:28am UTC](https://discuss.zetetic.net/t/change-password-of-the-database/509/3 "2015-04-10T03:28:35Z")

</div>

Thanks for that Nick…GOT IT WORKING

---

<div class="post-metadata">

**Author:** ![dimaslanjaka](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/dimaslanjaka/32/2198_2.png) [@dimaslanjaka](https://discuss.zetetic.net/u/dimaslanjaka)\
**Post date:** [March 27, 2024, 9:35am UTC](https://discuss.zetetic.net/t/change-password-of-the-database/509/4 "2024-03-27T09:35:36Z")

</div>

Hi guys, i have this snippet from tabnine AI. is below snippet working to change password of db ?

```auto
    @Synchronized
    fun changePassword(context: AppCompatActivity, dbPrefixName: String, previousPassword: String, newPassword: String) {
      val factory = SupportOpenHelperFactory(previousPassword.toByteArray(StandardCharsets.UTF_8))
      val id = DeviceInfo.cachedAndroidId
      dbLocation =
        FileManager(context).getAppFile("databases/${dbPrefixName}${id}" + (if (BuildConfig.DEBUG) ".sql" else "")).absolutePath
      val database =
        Room.databaseBuilder(context, DLRoomDatabase::class.java, dbLocation).openHelperFactory(factory).build()
      database.query("PRAGMA rekey = '$newPassword';", emptyArray())
      // finish activity
      context.finishAffinity()
    }

```

- Android room database
- SQLCipher android
