Python SQLite: database is locked

后端 未结 20 1154
一生所求
一生所求 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 15:14

    I'm presuming you are actually using sqlite3 even though your code says otherwise. Here are some things to check:

    1. That you don't have a hung process sitting on the file (unix: $ fuser cache.db should say nothing)
    2. There isn't a cache.db-journal file in the directory with cache.db; this would indicate a crashed session that hasn't been cleaned up properly.
    3. Ask the database shell to check itself: $ sqlite3 cache.db "pragma integrity_check;"
    4. Backup the database $ sqlite3 cache.db ".backup cache.db.bak"
    5. Remove cache.db as you probably have nothing in it (if you are just learning) and try your code again
    6. See if the backup works $ sqlite3 cache.db.bak ".schema"

    Failing that, read Things That Can Go Wrong and How to Corrupt Your Database Files

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

    Set the timeout parameter in your connect call, as in:

    connection = sqlite.connect('cache.db', timeout=10)
    
    0 讨论(0)
提交回复
热议问题