I\'m trying this code:
import sqlite
connection = sqlite.connect(\'cache.db\')
cur = connection.cursor()
cur.execute(\'\'\'create table item
(id integer p
Easy solution: check once if you have opened the database in another window or in another terminal. That also locks your database. In my case, I closed all the other terminals that were locking the database (a terminal tab in Pycharm). Check each tab of the terminals of your IDE as well if there is a terminal that left the database open. exit() all the terminals should work unlocking the database.
Turned out the problem happened because the path to the db file was actually a samba mounted dir. I moved it and that started working.
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 the try
block and when an exception was raised I wanted to do something else in the except
block.
try:
conn = sqlite3.connect(path)
cur = conn.cursor()
cur.execute('''INSERT INTO ...''')
except:
conn = sqlite3.connect(path)
cur = conn.cursor()
cur.execute('''DELETE FROM ...''')
cur.execute('''INSERT INTO ...''')
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.
try:
with sqlite3.connect(path) as conn:
cur = conn.cursor()
cur.execute('''INSERT INTO ...''')
except:
with sqlite3.connect(path) as conn:
cur = conn.cursor()
cur.execute('''DELETE FROM ...''')
cur.execute('''INSERT INTO ...''')
I also had this problem. I was trying to enter data into the database without saving changes I had made in it. after i saved the changes worked
The reason mine was showing the "Lock" message was actually due to me having opened an SQLite3 IDE on my mac and that was the reason it was locked. I assume I was playing around with the DB within the IDE and hadn't saved the changes and therefor a lock was placed.
Cut long story short, check that there are no unsaved changes on the db and also that it is not being used elsewhere.
in my case ,the error happened when a lot of concurrent process trying to read/write to the same table. I used retry to workaround the issue
def _retry_if_exception(exception):
return isinstance(exception, Exception)
@retry(retry_on_exception=_retry_if_exception,
wait_random_min=1000,
wait_random_max=5000,
stop_max_attempt_number=5)
def execute(cmd, commit=True):
c.execute(cmd)
c.conn.commit()