1
votes

I have been using the sqlite-net-pcl plugin for some, but the play store now requires an app to target Android version 7 at a minimum.

When I target this (Target Android Versions - 7.1), the application cannot open/create the database it only works for the lower version of Android, such as 5. I'm aware that there were security changes that may have caused this but I've no idea how to address it.

I get a datapath for android like so Path.Combine(global::Android.OS.Environment.ExternalStorageDirectory.Path, filename);

Then when starting my app tries to open the database connection like so

string dbPath = DependencyService.Get<IFileHelper>().GetLocalFilePath("myDbName.db3"); var conn = new SQLiteAsyncConnection(dbPath);

I want to use the SQLiteAsyncConnection as that handles the table locks.

The exception is:

Unhandled Exception: SQLite.SQLiteException: Could not open database file: /storage/emulated/0/myDbName.db3 (CannotOpen)

I've also ensured I have read and write ticked for external storage permission. The plugin I am using is https://github.com/praeclarum/sqlite-net which is what xamarin forms recommend in their documentation.

2
All is working fine from my side after updating to android 8 has target version. Is your sqllite nuget package up to date ?OrcusZ
Thanks for the reply!user3737964

2 Answers

0
votes

I think what you are trying to do is save the database file to an external storage location. You'll need to grant the app permissions to write to external storage (AppManifest -> Read/Write External Storage). Once you've done that, you can enable the permissions in the device settings (Apps->[app name]->Permissions) for testing purposes, or you can use the Permissions Plugin by James Montemagno to enable Read/Write to storage. The permissions will have to be granted before accessing the database file.

I too am using the same plugin. I have set up the database initialisation (after requesting storage permissions) as follows:

/* Request storage permissions first!! */

// After granting storage permissions...
string dbPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
string databaseFileName = "MyDatabase.db";
string fullDatabasePath = System.IO.Path.Combine(dbPath, databaseFileName);
_connection = new SQLiteConnection(fullDatabasePath);

I have not tried using SQLiteAsyncConnection, but it shouldn't make much difference.

0
votes

I think a sqlite database file is not something the user has to see and have access to it. So don't store it on a local folder.

As I said already on a similar reddit post.. You can skip all the permissions drama if you store the DB in a personal folder of the application. Then you don't have to ask for permissions. Check this official article, which explains permissions on different android version and especially the graph at the end.