0

I'm using this section of this official MSDN tutorial: Use a SQLite database in a UWP app but I'm getting the following error:

REMARK: There are many online posts related (or similar) to this issue but none seems to have a solution. Most of these posts are a few years old so I thought this issue would have been resolved by now. Moreover, the above mentioned tutorial is using .NET Standard Class library project, as well. And the online posts regarding the issue do not have .NET Standard involved. So, I was wondering if the issue is caused by the use of .NET Standard library. Regardless, a solution will be greatly appreciated.

SQLite Error 14: 'unable to open database file'

Error occurs at line db.Open() of this code:

public static void InitializeDatabase()
{
    using (SqliteConnection db =
        new SqliteConnection("Filename=sqliteSample.db"))
    {
        db.Open();

        String tableCommand = "CREATE TABLE IF NOT " +
            "EXISTS MyTable (Primary_Key INTEGER PRIMARY KEY, " +
            "Text_Entry NVARCHAR(2048) NULL)";

        SqliteCommand createTable = new SqliteCommand(tableCommand, db);

        createTable.ExecuteReader();
    }
}

NOTES:

  1. The line just below the above code reads: This code creates the SQLite database and stores it in the application's local data store. That means the app should have access to that local data store.
  2. I'm using latest version 16.3.5 of VS2019 on Windows 10. The target version on the project is selected as Windows 10 1903 and min version as Windows 10 1903

UPDATE

  1. This similar official 3 years old sample works fine. So, the problem seems to be related to newer versions of .NET Core. But I need to use latest version of .NET Core for other features my app is using that are not available in the older versions.
  2. I also tried this similar old tutorial, but it did not on new version of .NET Core work either - giving exact same error.
  3. The old problem reported in 2016 here to Microsoft seems to have resurfaced again with the new version of .NET Core.

1 Answer 1

1

This is a misunderstanding, SqliteConnection db = new SqliteConnection("Filename=sqliteSample.db") can not create a Sqlite file, but access the existing Sqlite database file through the path.

So you need to create a valid sqliteSample.db file and place it in the root directory of the UWP project. Select the content in the Properties -> Build operation to ensure it will be loaded into the application directory.

Update

Please create the sqliteSample.db file in LocalFolder first.

await ApplicationData.Current.LocalFolder.CreateFileAsync("sqliteSample.db", CreationCollisionOption.OpenIfExists);

Then use the path to access the database file

string path = Path.Combine(ApplicationData.Current.LocalFolder.Path, "sqliteSample.db");
using (SqliteConnection db =
    new SqliteConnection($"Filename={path}"))
{
    // ...
}

Best regards.

7
  • I tried your suggestion. Now I get this error on line createTable.ExecuteReader(); of the code: Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 8: attempt to write a readonly database'.'. I went to Windows explorer to checked the permissions of the db file and it is NOT read-only (that is, it is write-able).
    – nam
    Oct 17, 2019 at 18:11
  • The reason for this problem is that the sqlite database file built into the UWP project will be placed in a folder during the generation process, and the files in the folder cannot be modified. I am sorry for this problem, I have modified the answer, please check Oct 18, 2019 at 1:15
  • @RichardZhangMSFT Your updated suggestion would work if the project is not using .NET Standard class library DataAccess. But since SQLite related code is to be included in the DataAccess library, how do we use ApplicationData class there since there is no Windows.Storage namespace there. Please advise.
    – nam
    Oct 18, 2019 at 4:11
  • 1
    Hi, You don't have to put the process of creating a file in the class library. For example, you can create a file on UWP app and pass the file path parameter to the InitializeDatabase() method to initialize the database in this way. Oct 18, 2019 at 4:15
  • 1
    Hi @nam , We have updated the documentation on creating a Sqlite database, this is new content, thank you. Nov 6, 2019 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.