I\'m trying this code:
import sqlite
connection = sqlite.connect(\'cache.db\')
cur = connection.cursor()
cur.execute(\'\'\'create table item
(id integer p
You should check out if there is no DBMS administration and development platform working on your database (like pgAdmin), as this is probably the most popular cause of this error. If there is - commit the changes done and the problem is gone.
The database is locked by another process that is writing to it. You have to wait until the other transaction is committed. See the documentation of connect()
Here's a neat workaround for simultaneous access:
while True:
connection = sqlite3.connect('user.db', timeout=1)
cursor = connection.cursor()
try:
cursor.execute("SELECT * FROM queue;")
result = cursor.fetchall()
except sqlite3.OperationalError:
print("database locked")
num_users = len(result)
# ...
One possible reason for the database being locked that I ran into with SQLite is when I tried to access a row that was being written by one app, and read by another at the same time. You may want to set a busy timeout in your SQLite wrapper that will spin and wait for the database to become free (in the original c++ api the function is sqlite3_busy_timeout). I found that 300ms was sufficient in most cases.
But I doubt this is the problem, based on your post. Try other recommendations first.
In Linux you can do something similar, for example, if your locked file is development.db:
$ fuser development.db This command will show what process is locking the file:
development.db: 5430 Just kill the process...
kill -9 5430 ...And your database will be unlocked.
Because this is still the top Google hit for this problem, let me add a possible cause. If you're editing your database structure and haven't committed the changes, the database is locked until you commit or revert.
(Probably uncommon, but I'm developing an app so the code and database are both being developed at the same time)