Search code examples
windows-store-appssqlite-net

Could not open database file Windows Store App 8.1


I'm trying to implement a database in my Windows 8.1 Modern UI application. I made this for a Windows Phone 8.1 app successfully, but it doesn't work in a new Windows 8.1 app.

I got a SQLiteException with message Could not open database file: MyAppBase.db (CannotOpen) when i instanciate SQLiteConnection

public static DatabaseHelper Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new DatabaseHelper();
            }

            return _instance;
        }
    }

    public DatabaseHelper()
    {
        _connection = new SQLiteConnection(DATABASE_NAME);

        _connection.CreateTable<UserEntity>();
    }

I follow this steps : Added 'sqlite-net' to Nuget reference, Checked 'SQLite for Windows Runtime (Windows 8.1)' and 'Microsoft Visual C++ 2013 Runtime Package for Windows' on project references, Targetted build to x86.

How can i get this working ?


Solution

  • SQLite needs a path to create a DB not just a db name

    try something like this

    string path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, DATABASE_NAME);
    
    _connection = new SQLite.SQLiteConnection(path)
    

    Hope this helps