Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite attempt to write a readonly database error

Tags:

c#

sqlite

I have a console application that populates a SQLite database. When the application runs by itself, I don't get any errors. If I run multiple instances of the application, where each application is in its own folder and each populates its own database I'll occasionally get the following exception:

System.Data.SQLite.SQLiteException (0x80004005): Attempt to write a read-only database
attempt to write a readonly database
   at System.Data.SQLite.SQLite3.Reset(SQLiteStatement stmt)
   at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt)
   at System.Data.SQLite.SQLiteDataReader.NextResult()
   at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
   at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()

I know the DB isn't read-only because it has already written data to that database. Also, the app continues on and will continue populating data to that database. I haven't reproduced the error when only a single instance of the application is running.

I've tried using the pragma to have both the journal and temp_store in memory instead of to a file in case there could be some contention across applications there, but I still get the error. I do always get the error in the same method, which is the first time an insert would happen for the connection. To give a general idea of what the application does, it loops over cases, gathers info about those cases (not from SQLite), then writes results to the SQLite database.

I don't know what else to try.

*edit I am also using the PRAGMA journal_mode=MEMORY. When inserting data into the SQLite database, I first run a BEGIN statement, then a bunch of inserts before the END statement. The error occurs on the first insert.

like image 341
UWSkeletor Avatar asked Aug 04 '15 21:08

UWSkeletor


2 Answers

It is a permissions issue.

Make sure that your web application that is hosted has add/write/create/delete access to the folder where the sqlite database resides.

For more details, refer this

like image 148
LakshmiNarayanan Avatar answered Sep 23 '22 06:09

LakshmiNarayanan


Could also be that The database file is not supported by the MigrationAssembly.

The same exception is thrown when the EF tries to migrate on a file expected to be a SQLite database which is fi. a text file.

like image 40
Jostein Sætherø Avatar answered Sep 23 '22 06:09

Jostein Sætherø