Can't create an SQLite table on Android

Hi guys, I have been following this tutorial in its entirety, on how to store data in a local database. On iOS everything works fine, unfortunately on android i get an exception upon creating a table in my database file.. The method for creating the table is:

public CalcDatabase(string dbPath)
{
  database = new SQLiteAsyncConnection(dbPath);
  database.CreateTableAsync<Calculation>().Wait();
}

And my custom class for getting the database path on Android is as follows:

public class LocalFileHelper : IFileHelper
{
        public string GetLocalFilePath(string filename)
        {
            var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            path = Path.Combine(path, filename);

            if(!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            return path;
        }
    }

This is the error that im getting from SQLite:

Could not open database file: /data/user/0/com.companyname.XX/files/Calculation.db3 (CannotOpen)

I have tried targeting several different APIs, i have tried to clean and rebuild my solution, and i have tried to reinstall the SQLite-net-pcl NuGet packages across the whole solution.. Nothing works..

  • I have read, that it has to do with permissions, still i do however find it strange that the official microsoft tutorial doesn't cover this, if it was a thing..

Best Answer

Answers

  • JustinBebberJustinBebber Member ✭✭

    Thank you for your answer @Ahsan_Siddique. For some strange reason I once again cleaned my solution, rebuilt it and added the following two permissions to my AndroidManifest.xml once again:

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

    This time it works.. Very strange!

Sign In or Register to comment.