I'm trying this code:
import sqlite
connection = sqlite.connect('cache.db')
cur = connection.cursor()
cur.execute('''create table item
(id integer primary key, itemno text unique,
scancode text, descr text, price real)''')
connection.commit()
cur.close()
I'm catching this exception:
Traceback (most recent call last):
File "cache_storage.py", line 7, in <module>
scancode text, descr text, price real)''')
File "/usr/lib/python2.6/dist-packages/sqlite/main.py", line 237, in execute
self.con._begin()
File "/usr/lib/python2.6/dist-packages/sqlite/main.py", line 503, in _begin
self.db.execute("BEGIN")
_sqlite.OperationalError: database is locked
Permissions for cache.db are ok. Any ideas?
cache.db
is being currently used by another process.Set the timeout parameter in your connect call, as in:
I'm presuming you are actually using sqlite3 even though your code says otherwise. Here are some things to check:
$ fuser cache.db
should say nothing)$ sqlite3 cache.db "pragma integrity_check;"
$ sqlite3 cache.db ".backup cache.db.bak"
$ sqlite3 cache.db.bak ".schema"
Failing that, read Things That Can Go Wrong and How to Corrupt Your Database Files
I had the same problem:
sqlite3.IntegrityError
As mentioned in many answers, the problem is that a connection has not been properly closed.
In my case I had
try
except
blocks. I was accessing the database in thetry
block and when an exception was raised I wanted to do something else in theexcept
block.However, when the exception was being raised the connection from the
try
block had not been closed.I solved it using
with
statements inside the blocks.