17

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.

2
  • "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" There won't be. How are you opening the database? Is the folder the database is in writeable? Commented Aug 5, 2015 at 14:11
  • 1
    Yes, the folder the database is in is writeable. Most iterations are properly inserting data into the database. I'm opening the database just through a using statement around the SQLiteConnection. Commented Aug 5, 2015 at 14:37

2 Answers 2

12

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but how can it be a permissions issue if it is random? The application successfully writes to the database. It is only on random iterations that it gets this error.
I agree with @UWSkeletor. What else?
This has worked for my scenario.. The service was running under www-data user, the DB file was created under my custom user. The solution of adding the custom user to www-data group seems to have resolved the problem (at least for now) usermod -aG www-data custom_user. Also I am not sure if it's related, but I am using the journal mode PRAGMA journal_mode=WAL; for the sqlite db which presumably helps with concurrency issues, which might, or might not be related to the issues the author was having. So I recommend using this mode.
1

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.