简体   繁体   中英

I'm receiving “SQLite Error 14: 'unable to open database file'.”

I am writing an ASP.NET web API on .NET Framework 4.7.2. I would like to have a SQLite database in a file on my harddrive. Then I could access the database using a SqliteConnection.

In my code, I call var db = new SqliteConnection("Data Source=testDB.sqlite"); to create the SqliteConnection. Then I call db.Open(); . This function throws a Microsoft.Data.Sqlite.SqliteException containing the error "SQLite Error 14: 'unable to open database file'."

I tried to create the testDB.sqlite file manually, but I wasn't sure where to put it. I am not asking the SqliteConnection to create any directories. I am not writing a UWP app (because ASP.NET is different from UWP, correct?). I've tried both a relative path and an absolute path. I even deleted the entire project and used the Visual Studio project creation wizard to regenerate it. I do not know what else to try.

I've created a minimal reproducible example here . You should be able to just pop this right into Visual Studio. Besides the plumbing, the example is literally only 2 lines long. Does anyone have any ideas what I'm doing wrong?

For your project try to install dependece: Microsoft.EntityFrameworkCore.Sqlite and correct connection string in your code like this:

var db = new SqliteConnection("Data Source=file:testDB.sqllite;Mode=ReadWrite;");
db.Open();

Microsoft.Data.Sqlite is using filename in Data Sourse as URI: https://www.sqlite.org/c3ref/open.html#urifilenamesinsqlite3open

I created sqllite database with SQLliteStudio: https://github.com/pawelsalawa/sqlitestudio/releases

Added testDB.sqllite in project and set property "Copy to Output directory":"Copy if newer".

After those steps "Open" worked success

In my case, I'd accidentally mixed up the versions of the Nuget packages added to projects within my solution.

I had a reference to the following inside the .csproj :

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.1"
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.1" />

Updateding packages to:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.10"
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.10" />

Resolved this error for us inside a UWP project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM