121
votes

How can I change an SQLite database from read-only to read-write?

When I executed the update statement, I always got:

SQL error: attempt to write a readonly database

The SQLite file is a writeable file on the filesystem.

17
Does the user running sqlite3 (or whatever you are using to execute the query) have write permissions to the db? Have you double checked file ownership? - Tim Post♦
I'm sure they have permission to do it. - user143482
I've seen this in a web app where I forgot to set the GID on the database file and the "www-data" account (that Apache runs under) was refused write access to the file. - finnw

17 Answers

102
votes

There can be several reasons for this error message:

  • Several processes have the database open at the same time (see the FAQ).

  • There is a plugin to compress and encrypt the database. It doesn't allow to modify the DB.

  • Lastly, another FAQ says: "Make sure that the directory containing the database file is also writable to the user executing the CGI script." I think this is because the engine needs to create more files in the directory.

  • The whole filesystem might be read only, for example after a crash.

  • On Unix systems, another process can replace the whole file.

13
votes

I solved this by changing owner from root to me on all files on /db dir.

Just do ls -l on that folder, if any of the filer is owned by root just change it to you, using: sudo chown user file

7
votes

(this error message is typically misleading, and is usually a general permissions error)

On Windows

  • If you're issuing SQL directly against the database, make sure whatever application you're using to run the SQL is running as administrator
  • If an application is attempting the update, the account that it uses to access the database may need permissions on the folder containing your database file. For example, if IIS is accessing the database, the IUSR and IIS_IUSRS may both need appropriate permissions (you can try this by temporarily giving these accounts full control over the folder, checking if this works, then tying down the permissions as appropriate)
6
votes

This error usually happens when your database is accessed by one application already, and you're trying to access it with another application.

4
votes

If using Android.

Make sure you have added the permission to write to your EXTERNAL_STORAGE to your AndroidManifest.xml.

Add this line to your AndroidManifest.xml file above and outside your <application> tag.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

This will allow your application to write to the sdcard. This will help if your EXTERNAL_STORAGE is where you have stored your database on the device.

4
votes

In Linux command shell, I did:

chmod 777 <db_folder>

Where contains the database file.

It works. Now I can access my database and make insert queries.

4
votes

To share personal experience I encountered with this error that eventually fix both. Might not necessarily be related to your issue but it appears this error is so generic that it can be attributed to gazillion things.

  1. Database instance open in another application. My DB appeared to have been in a "locked" state so it transition to read only mode. I was able to track it down by stopping the a 2nd instance of the application sharing the DB.

  2. Directory tree permission - please be sure to ensure user account has permission not just at the file level but at the entire upper directory level all the way to / level.

Thanks

2
votes

I had this problem today, too.

It was caused by ActiveSync on Windows Mobile - the folder I was working in was synced so the AS process grabbed the DB file from time to time causing this error.

2
votes

On Windows:

tl;dr: Try opening the file again.

Our system was suffering this problem, and it definitely wasn't a permissions issue, since the program itself would be able to open the database as writable from many threads most of the time, but occasionally (only on Windows, not on OSX), a thread would get these errors even though all the other threads in the program were having no difficulties.

We eventually discovered that the threads that were failing were only those that were trying to open the database immediately after another thread had closed it (within 3 ms). We speculated that the problem was due to the fact that Windows (or the sqlite implementation under windows) doesn't always immediately clean up up file resources upon closing of a file. We got around this by running a test write query against the db upon opening (e.g., creating then dropping a table with a silly name). If the create/drop failed, we waited for 50 ms and tried again, repeating until we succeeded or 5 seconds elapsed.

It worked; apparently there just needed to be enough time for the resources to flush out to disk.

2
votes

On Ubuntu, change the owner to the Apache group and grant the right permissions (no, it's not 777):

sudo chgrp www-data <path to db.sqlite3>
sudo chmod 664 <path to db.sqlite3>

Update

You can set the permissions for group and user as well.

sudo chown www-data:www-data <path to db.sqlite3>
1
votes

On Linux, give read/write permissions to the entire folder containing the database file.

Also, SELinux might be blocking the write. You need to set the correct permissions.

In my SELinux Management GUI (on Fedora 19), I checked the box on the line labelled httpd_unified (Unify HTTPD handling of all content files), and I was good to go.

0
votes

Edit the DB: I was having problems editing the db. I ended up having to
sudo chown 'non root username' ts3server.sqlitedb
as long as it wasn't root, i could edit the file. Username is the username of my non root account.

Auto start TeamSpeak: as your non root account
crontab -e
@reboot /path to ts3server/ aka /home/ts3server/ts3server_startscript.sh start

0
votes

In the project path Terminal django_project#

sudo chown django:django *
0
votes

I'm using SQLite on ESP32 and all answers here are "very strange".... When I look at the data on the flash of the ESP I notice there is only one file for the whole db (there is also a temp file).

In this db file we have of course the user tables but also the system tables so "sqlite_master" for example which contain the definiton of the tables. So, it's seems hard to belive this can be a "chmod" problem, because if the file is read only, even creating table would be impossible as SQLite would be unable to write the "sqlite_master" data... So I think our friend user143482 is trying to acesse a "read only" table. In SQLite source code we can see a function named tabIsReadOnly with this comment:

 /* Return true if table pTab is read-only.
 **
 ** A table is read-only if any of the following are true:
 **
 **   1) It is a virtual table and no implementation of the xUpdate method
 **      has been provided
 **
 **   2) It is a system table (i.e. sqlite_master), this call is not
 **      part of a nested parse and writable_schema pragma has not 
 **      been specified
 **
 **   3) The table is a shadow table, the database    connection is in
 **      defensive mode, and the current sqlite3_prepare()
 **      is for a top-level SQL statement.
 */
0
votes

On win10 after a system crash, try to open db with DB Browser, but read only. Simply delete the journal file.

0
votes

After hours of hit and trial, I solved my issue. Even though I had changed my permissions (used chmod 777 db.sqlite3 as well, sadly), but the issue was something else altogether.

Finally this thing worked (probably because I used bitnami)

$ chown :daemon /path/to/your/sqlite/file

$ chmod 664 /path/to/your/sqlite/file

$ chown :daemon /path/to/your/project

$ chmod 775 /path/to/your/project

-1
votes

From the command line, enter the folder where your database file is located and execute the following command:

chmod 777 databasefilename

This will grant all permissions to all users.