2

my app is working fine so far but on some devices im getting an error (Android 4.1 or 4.3 for example )

 android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
    at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
    at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
    at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
    at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:235)
    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)

In Class DBManager i have this:

public class DBManager {
    public DBHelper helper;
    private SQLiteDatabase db;
    private Cursor c;

    public DBManager(Context context) {
        helper = new DBHelper(context);
        db = helper.getReadableDatabase();

       // db = helper.getWritableDatabase();
    }

And in DBHelper this:

 public class DBHelper extends SQLiteOpenHelper {
        private static final String DATABASE_NAME = Environment.getExternalStorageDirectory() + "/database/database.db3";
 private static final int DATABASE_VERSION = 3;

 public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_SQL);

    }

Manifest etc. is ok but i do not understand why on some devices the SQLiteCantOpenDatabaseException comes . I hope somebody can help me to solve this problem ? Thanx in advance

Frank
  • 2,738
  • 2
  • 14
  • 19

1 Answers1

0

You are setting DATABASE_NAME to a path rooted at the directory returned by Environment.getExternalStorageDirectory().

To quote the documentation for getExternalStorageDirectory():

This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened.

If you do not really need to specify the location of the database file, you should just specify the file name (e.g., "MyDB"), and the Android framework will determine an appropriate path for you.

[UPDATED]

Usually, a DB (which may contain very important data, and may even contain sensitive user information) is NOT stored in external storage, since:

  1. The file would be accessible to other apps, and
  2. The file can be accidentally corrupted/deleted by the user.

If you want to create a backup copy of the DB (and put it in external storage), see this answer.

Community
  • 1
  • 1
cybersam
  • 63,203
  • 6
  • 53
  • 76
  • Hi yes but my intention is to store it there with getExternalStorageDirectory() e.g. in /storage/emulated/0/database/databes.db3 ...because so the user can everytime make a backup from this database . Maybe i would guess the error occurs on some devices because the path does not exist on the creation of the database ? – Frank Jan 22 '16 at 02:23