2

I'm trying to load a Database file from my SD card,but is giving exception. Below is the 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");
}

These are the permission i'm using

<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 out... Thanks

This is the path in sdcard

Yushi
  • 416
  • 6
  • 24

5 Answers5

3

This exception is thrown if sqlite3_db_readonly() returns non-zero. It can return non-zero if

  • the database file is read-only, or

  • the database file does not exist.

(Reference)

You have a hardcoded path "/storage/sdcard1/sk2.db" - it's likely a database does not exist there. Use variables from Environment to access your external storage instead of hardcoded paths.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • Hi laalto It is working fine in emulator but when i run this in a device its show an exception.Database exist in that particular path – Yushi Feb 10 '14 at 13:00
  • In the ddms file explorer screenshot, all user permissions are not set : `----rwxr-x` instead of e.g. `-rwxrwxr-x`. – laalto Feb 10 '14 at 13:05
0

add this permission to your AndroidManifest.xml.

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

Pankaj Kharche
  • 1,329
  • 4
  • 15
  • 37
0

please use both permission in manifest file

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Sagar Devkota
  • 1,192
  • 8
  • 13
0

In case you still get this error even after you included:

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

and you also implemented Runtime permissions then it almost certainly is a problem related to the way Android manages files from External SD Card. For more info check my answer here:

https://stackoverflow.com/a/46383795/1502079

vovahost
  • 34,185
  • 17
  • 113
  • 116
0

if your sdk>N ,it must request write and read permission like this

ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE},
            1);
chen Aron
  • 1
  • 1