-3

I have an application that creates a database file at '/storage/emulated/0/databases/mydb.db' and there are files within this DB. I have another application which tries to open this database. But I get error code 14. Could not open database when trying to oprn the table in this db.

public class DataBaseHelper extends SQLiteOpenHelper {

public SQLiteDatabase myDataBase;
public String TABLE_NAME;
// Database Information
static final String DB_NAME = "mydb.DB";
public static final String FILE_DIR = "databases";
private String DB_PATH = Environment.getExternalStorageDirectory()
        + File.separator + FILE_DIR + File.separator + DB_NAME;

public DataBaseHelper(Context context, String name) {
    super(context, name, null, 1);
    TABLE_NAME = name;
}

public void openDatabase() throws SQLiteException {
    String DBPath = DB_PATH + "/" +TABLE_NAME;
    myDataBase = SQLiteDatabase.openDatabase(DBPath,null,SQLiteDatabase.OPEN_READWRITE);
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

I get an error at myDataBase = SQLiteDatabase.openDatabase(DBPath,null,SQLiteDatabase.OPEN_READWRITE);

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
d.sanghvi
  • 47
  • 2
  • 10
  • 2
    Did you check the permissions of the created file? Two different apps are always two different Linux users. – Jonas Köritz Jan 04 '17 at 08:57
  • I am not using a rooted phone. so default permissions. – d.sanghvi Jan 04 '17 at 09:00
  • You can share your database by either making a ContentProvider or by placing the database in a folder which is accessible from both the apps (and potentially to all your installed apps). – Phantômaxx Jan 04 '17 at 10:54
  • @Rotwang yes, so I have created the database in external storage, to make it accessible from all other apps. But it still gives error. – d.sanghvi Jan 04 '17 at 13:22
  • `/storage/emulated/0/` is not present on every device at this exact location. Instead of harcoding your path, you should get the path to the external storage by using the proper `Environment` methods – Phantômaxx Jan 04 '17 at 13:57
  • I have created it at: Environment.getExternalStorageDirectory() + File.separator + FILE_DIR + File.separator + DB_NAME; and I am trying to access it from: Environment.getExternalStorageDirectory() + File.separator + FILE_DIR + File.separator + DB_NAME; – d.sanghvi Jan 06 '17 at 08:53

2 Answers2

1

Another thing to note is Environment.getExternalStorageDirectory() has been deprecated in API 29:

https://developer.android.com/reference/android/os/Environment#getExternalStorageDirectory()

This method was deprecated in API level 29.

To improve user privacy, direct access to shared/external storage devices is deprecated. When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps. Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT.

Community
  • 1
  • 1
Chin
  • 19,717
  • 37
  • 107
  • 164
0

1. Permission

Your app needs file read permission. Above Android 6.0, you need to request runtime permission.

See this: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database


2. Wrong path

It seems that the path

/storage/emulated/0/databases/mydb.db

is not same as path below.

Environment.getExternalStorageDirectory() + File.separator + FILE_DIR + File.separator + DB_NAME

So, before open database file, compare two paths to check it is same.

if (DB_PATH.compareTo("/storage/emulated/0/databases/mydb.db")){
    // Do something
}

And check the to find out it really exists.

File file = new File(myPath);    
if (file.exists() && !file.isDirectory()) {
    // Do something
}

Details: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database trouble

Community
  • 1
  • 1
Stanley Ko
  • 3,383
  • 3
  • 34
  • 60