Python SQLite: database is locked

后端 未结 20 1152
一生所求
一生所求 2020-12-12 14:24

I\'m trying this code:

import sqlite

connection = sqlite.connect(\'cache.db\')
cur = connection.cursor()
cur.execute(\'\'\'create table item
  (id integer p         


        
相关标签:
20条回答
  • 2020-12-12 14:49

    Even when I just had one writer and one reader, my issue was that one of the reads was taking too long: longer than the stipulated timeout of 5 seconds. So the writer timed out and caused the error.

    So, be careful when reading all entries from a database especially from one which the size of the table grows over time.

    0 讨论(0)
  • 2020-12-12 14:52

    I know this is old, but I'm still getting the problem and this is the first link on Google for it. OP said his issue was that the .db was sitting on a SMB share, which was exactly my situation. My ten minutes' research indicates that this is a known conflict between sqlite3 and smb; I've found bug reports going back to 2007.

    I resolved it by adding the "nobrl" option to my smb mount line in /etc/fstab, so that line now looks like this:

    //SERVER/share /mnt/point cifs credentials=/path/to/.creds,sec=ntlm,nobrl 0 0
    

    This option prevents your SMB client from sending byte range locks to the server. I'm not too up on my SMB protocol details, but I best I can tell this setting would mostly be of concern in a multi-user environment, where somebody else might be trying to write to the same db as you. For a home setup, at least, I think it's safe enough.

    My relevant versions:

    • Mint 17.1 Rebecca
    • SMB v4.1.6-Ubuntu
    • Python v3.4.0
    • SQLite v3.8.2
    • Network share is hosted on a Win12R2 server
    0 讨论(0)
  • 2020-12-12 14:52

    I had this problem while working with Pycharm and with a database that was originally given to me by another user.

    So, this is how I solve it in my case:

    1. Closed all tabs in Pycharm that operate with the problematic database.
    2. Stop all running processes from the red square botton in the top right corner of Pycharm.
    3. Delete the problematic database from the directory.
    4. Upload again the original database. And it worked again.
    0 讨论(0)
  • 2020-12-12 14:53

    Oh, your traceback gave it away: you have a version conflict. You have installed some old version of sqlite in your local dist-packages directory when you already have sqlite3 included in your python2.6 distribution and don't need and probably can't use the old sqlite version. First try:

    $ python -c "import sqlite3"
    

    and if that doesn't give you an error, uninstall your dist-package:

    easy_install -mxN sqlite
    

    and then import sqlite3 in your code instead and have fun.

    0 讨论(0)
  • 2020-12-12 14:53

    I found this worked for my needs. (thread locking) YMMV

    conn = sqlite3.connect(database, timeout=10)

    https://docs.python.org/3/library/sqlite3.html

    sqlite3.connect(database[, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements, uri])

    When a database is accessed by multiple connections, and one of the processes modifies the database, the SQLite database is locked until that transaction is committed. The timeout parameter specifies how long the connection should wait for the lock to go away until raising an exception. The default for the timeout parameter is 5.0 (five seconds).

    0 讨论(0)
  • 2020-12-12 14:55
    1. Your cache.db is being currently used by another process.
    2. Stop that process and try again, it should work.
    0 讨论(0)
提交回复
热议问题