0

I'm trying to read a .db file with SQlite. I am using android 11 and the new file handling system. I have already made the request for permissions for the specific folder and they work fine since I can read and write in other .txt files but in the .db I can't.

if (dbFile.exists() && !dbFile.isDirectory()) {
dbSQLite = SQLiteDatabase.openDatabase(dbFile.getUri().getPath(), null, SQLiteDatabase.OPEN_READONLY);
}

when dbFile.getUri().getPath() -> /tree/primary:dia/document/primary:dia/in/catalog.db and file exist.

The call to open the database throws me an exception that I catch:

Method threw 'android.database.sqlite.SQLiteCantOpenDatabaseException' exception.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14 SQLITE_CANTOPEN): Could not open database
Cannot open database '/tree/primary:dia/document/primary:dia/in/catalog.db': Directory /tree/primary:dia/document/primary:dia/in doesn't exist

Thank you very much for your time and sorry for my English

Martis
  • 1
  • 1
  • A `Uri` is not a file. You will need to copy the content of the user-selected database to a file that you can access. – CommonsWare Oct 13 '21 at 12:37
  • Hello, thank you for your contribution now my question is how do I create a file if with android 11 they cannot be used and they advise to use DocumentFile? – Martis Oct 13 '21 at 13:20
  • "how do I create a file" -- create it in a place where you have read/write filesystem access. For a SQLite database, you might find such a location via `getDatabasePath()`. Or, use `getFilesDir()` (both are methods on `Context`). Then, open an `InputStream` using `ContentResolver` and `openInputStream()` on the content identified by the `Uri`. Copy the bytes to your desired file, such as via an `OutputStream`. Flush/sync/close the `OutputStream`, and now you have a file to use with SQLite. – CommonsWare Oct 13 '21 at 13:28
  • 1
    Thank you very much I managed to solve my problem thanks to your answer !! : D – Martis Oct 14 '21 at 06:56

0 Answers0