# Not an error: Could not open the database in read/write mode

**URL:** <https://discuss.zetetic.net/t/not-an-error-could-not-open-the-database-in-read-write-mode/6008>\
**Category:** SQLCipher\
**Created:** [July 13, 2023, 12:50pm UTC](https://discuss.zetetic.net/t/not-an-error-could-not-open-the-database-in-read-write-mode/6008 "2023-07-13T12:50:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JyothiSri](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@JyothiSri](https://discuss.zetetic.net/u/JyothiSri)\
**Post date:** [July 13, 2023, 12:50pm UTC](https://discuss.zetetic.net/t/not-an-error-could-not-open-the-database-in-read-write-mode/6008/1 "2023-07-13T12:50:58Z")

</div>

Hi,  
I have implemented Cipher(SQLCipher version 4.2.0) in my previous Sqlite3 db helper class, I have a old db data and i am trying to export old db file data to new db file, when i am opening db I’m getting error like-- not an error: Could not open the database in read/write mode.  
but CURD operations are working fine with newly created db file.  
SQLiteDatabase sqlDB = mDbHelper.getWritableDatabase(“abc”);  
SQLiteDatabase sqlDB = mDbHelper.getReadableDatabase(“abc”);

here is my code :  
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(oldDb.getPath(), “”, null);  
db.rawExecSQL(String.format(“ATTACH DATABASE ‘%s’ AS encrypted KEY ‘%s’;”,  
newDb.getAbsolutePath(), “abc”));  
db.rawExecSQL(“SELECT sqlcipher\_export(‘encrypted’)”);  
db.rawExecSQL(“DETACH DATABASE encrypted;”);  
int version = db.getVersion();  
db.close();  
db.setVersion(version);

Please help me out 🙁  
Thanks in advance.

---

<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:** [July 13, 2023, 1:25pm UTC](https://discuss.zetetic.net/t/not-an-error-could-not-open-the-database-in-read-write-mode/6008/2 "2023-07-13T13:25:36Z")

</div>

Hi @JyothiSri,

At what line of the code does the exception occur? Are you using the `android-database-sqlcipher` library, or `sqlcipher-android` library? We have an example import scenario within the `android-database-sqlcipher` test suite [here](https://github.com/sqlcipher/sqlcipher-android-tests/blob/master/app/src/main/java/net/zetetic/tests/ImportUnencryptedDatabaseTest.java), and another import example using `sqlcipher-android` [here](https://github.com/sqlcipher/sqlcipher-android/blob/master/sqlcipher/src/androidTest/java/net/zetetic/database/sqlcipher_cts/ImportUnencryptedDatabaseTest.java) which you might try running and comparing with your application.

As an aside, the latest version of SQLCipher is 4.5.4 (though this shouldn’t be related to your issue). You would need to set the database version from the old version to the new database; your code is reading and writing the same value on the old database.

---

<div class="post-metadata">

**Author:** ![JyothiSri](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@JyothiSri](https://discuss.zetetic.net/u/JyothiSri)\
**Post date:** [July 14, 2023, 5:51am UTC](https://discuss.zetetic.net/t/not-an-error-could-not-open-the-database-in-read-write-mode/6008/3 "2023-07-14T05:51:22Z")

</div>

Thanks for the reply.  
I am getting the error at this line–\>SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(oldDb.getPath(), “”, null);  
And I am using net.sqlcipher.database.SQLiteDatabase;  
And one more thing is latest versions are not supporting to my application.  
Is there any wrong step I did, same as above example I’m trying to openOrCreating old db with empty password here only I’m getting the error.

---

<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:** [July 14, 2023, 1:10pm UTC](https://discuss.zetetic.net/t/not-an-error-could-not-open-the-database-in-read-write-mode/6008/4 "2023-07-14T13:10:55Z")

</div>

Hi @JyothiSri,

A few follow-up questions:

- Where does `oldDb.getPath()` evaluate to?
- Is it old database file within the `databases` directory of the application?
- Have you tried `oldDb.getAbsolutePath()` instead?
- What are your results of running the [import example](https://github.com/sqlcipher/sqlcipher-android-tests/blob/master/app/src/main/java/net/zetetic/tests/ImportUnencryptedDatabaseTest.java) but using your source database instead?

> [@JyothiSri](#):
>
> And one more thing is latest versions are not supporting to my application.

In what way is 4.5.4 not supported in your application?

---

<div class="post-metadata">

**Author:** ![JyothiSri](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@JyothiSri](https://discuss.zetetic.net/u/JyothiSri)\
**Post date:** [July 18, 2023, 7:33am UTC](https://discuss.zetetic.net/t/not-an-error-could-not-open-the-database-in-read-write-mode/6008/5 "2023-07-18T07:33:10Z")

</div>

Hi,  
I am getting a old db path with getAbsolutePath() but I’m getting warning msg i.e “old db path is not found”,  
After i used source database I’m getting java.lang.IllegalStateException: database not open. first time it is working perfectly. if it is running 2nd time I,m getting above IllegalStateException.

---

<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:** [July 18, 2023, 1:13pm UTC](https://discuss.zetetic.net/t/not-an-error-could-not-open-the-database-in-read-write-mode/6008/6 "2023-07-18T13:13:57Z")

</div>

Hi @JyothiSri,

If the old database path does not exist, you will not be able to migrate it. You will need to debug your application further to determine whether the database file exists, then construct a path to it for your conversion process.
