Search code examples
sqliteuwp

Unable to open SQLite database file from local data store of UWP app: Microsoft.Data.Sqlite.Core 8.0.0


I'm using this section of this official MSDN tutorial: Use a SQLite database in a UWP app app but I'm getting the following Exception. I have followed everything as per the documentation. For reference I have added the exception and the code block of the class files and the sqlite version I'm using in the UWP application

Exception:

Exception thrown: 'System.Exception' in SQLitePCLRaw.core.dll
Exception thrown: 'System.TypeInitializationException' in Microsoft.Data.Sqlite.dll
Exception thrown: 'System.TypeInitializationException' in System.Private.CoreLib.dll
Exception thrown at 0x761F9392 (KernelBase.dll) in eula.exe: WinRT originate error - 0x80131534 : 'System.TypeInitializationException: The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception. ---> System.Exception: You need to call SQLitePCL.raw.SetProvider().  If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init().
   at SQLitePCL.raw.get_Provider()
   at SQLitePCL.raw.sqlite3_win32_set_directory(Int32 typ, String path)
   at Microsoft.Data.Sqlite.SqliteConnection..cctor()
   --- End of inner exception stack trace ---
   at Microsoft.Data.'.
Exception thrown at 0x761F9392 (KernelBase.dll) in eula.exe: WinRT originate error - 0x80131534 : 'The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception.'.

Package Installed: Microsoft.Data.Sqlite.Core 8.0.0

DataAccessService.cs:

public class DataAccessService
{
    public string pathToDatabase = string.Empty;
    private static DataAccessService _instance;
    public DataAccessService()
    {
        StorageFile storageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
            "sqliteEula.db",
            CreationCollisionOption.OpenIfExists
        );
        pathToDatabase = storageFile.Path;
    }
    public SqliteConnection GetNewConnection()
    {
        Debug.WriteLine($"Database: {pathToDatabase}");
        // Database: C:\Users\eula\AppData\Local\Packages\eula.hsb_hgksdyfgbh\LocalState\sqliteEula.db
        using (SqliteConnection connection = new SqliteConnection($"Filename={pathToDatabase}"))
        {
            try
            {
                connection.Open();
                return connection;
            }
            catch (Exception ex) { 
                Debug.WriteLine(ex.StackTrace);
            }
        }
        return null;
    }
    public static DataAccessService GetInstance()
    {
        if (_instance == null) _instance = new DataAccessService();
        return _instance;
    }
}

MainPage.xaml.cs:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        DataAccessService dataAccessService = DataAccessService.GetInstance()
    }
    private void DataAccess_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        SqliteConnection connection = dataAccessService.GetNewConnection();
    }
}

Solution

  • There is a small bug in the docs. You need to install Microsoft.Data.SQLite instead of Microsoft.Data.SQLite.Core.