Sqlite3.OperationalError: database is locked - non-streaming application

I have a Python application that throws a standard sqlite3.OperationalError: database is locked error. I searched the Internet and could not find any solution that worked (note that multiprocesses / threads do not occur, and as you can see, I tried to increase the timeout parameter). The sqlite file is stored on the local hard drive.

The following function is one of many that accesses the sqlite database and works great on the first call, but it throws the above error a second time (it is called part of the for loop in another function):

 def update_index(filepath): path = get_setting('Local', 'web') stat = os.stat(filepath) modified = stat.st_mtime index_file = get_setting('Local', 'index') connection = sqlite3.connect(index_file, 30) cursor = connection.cursor() head, tail = os.path.split(filepath) cursor.execute('UPDATE hwlive SET date=? WHERE path=? AND name=?;', (modified, head, tail)) connection.commit() connection.close() 

Many thanks.

+4
source share
2 answers

You can especially check for functions that retain a read lock (incomplete cursor). This blocks the commit from the update function. Please note that for Python-sqlite problems there is a dedicated mailing list: http://groups.google.com/group/python-sqlite

+2
source

Do you really need to constantly open and close the database file for each individual UPDATE? If you do the same in every function that accesses the database, is it possible that you called the update_index function from another function that already opened the database using a different connection and is in the process of modifying the database?

+1
source

Source: https://habr.com/ru/post/1305888/


All Articles