Failed to open the database in read / write mode

I am trying to download a database file from my SD card, but gives an exception. The following is an exception

org.sqlite.database.sqlite.SQLiteException: not an error (code 0): Could not open the database in read/write mode. 

And here is my code:

 public void csr_test_1() throws Exception 

{

 DB_PATH=new File("/storage/sdcard1/sk2.db"); SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null); String res = ""; Cursor c = db.rawQuery("SELECT synsetid, w2.lemma FROM sense LEFT JOIN word AS w2 ON w2.wordid=sense.wordid WHERE sense.synsetid IN (SELECT sense.synsetid FROM word AS w1 LEFT JOIN sense ON w1.wordid=sense.wordid WHERE w1.lemma='"+ "life" + "') AND w2.lemma<>'" + "life" + "'", null); if( c!=null ){ boolean bRes; for(bRes=c.moveToFirst(); bRes; bRes=c.moveToNext()){ String x = c.getString(0); res = res + "." + x; } }else{ } test_result("csr_test_1.1", res, ".one.two.three"); db.close(); test_result("csr_test_1.2", db_is_encrypted(), "unencrypted"); } 

This is the permission I use.

 <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="com.android.vending.BILLING" /> ![enter image description here][1] ![enter image description here][2] 

Please help me ... Thanks

This is the path in sdcard

+1
source share
4 answers

This exception is thrown if sqlite3_db_readonly() returns a nonzero value . It can return a nonzero value if

  • the database file is read-only or

  • The database file does not exist.

( Link )

You have a hardcoded path "/storage/sdcard1/sk2.db" - this is probably the database does not exist there. Use variables from Environment to access external storage instead of hard paths.

+3
source

add this permission to your AndroidManifest.xml .

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

0
source

use both permissions in the manifest file

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
0
source

If you still get this error even after you turned it on:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

and you also implemented Runtime permissions, then this is almost certainly a problem with how Android manages files from an external SD card. For more information check my answer here:

fooobar.com/questions/1250330 / ...

0
source

All Articles