Sqlite python sqlite3.OperationalError:数据库被锁定

时间:2014-11-27 00:03:20

标签: python multithreading debugging sqlite

我编写了以下代码,显示sqlite3.OperationalError: database is locked错误。任何有关调试的帮助都将非常感激。

基本上我试图将数据从table1复制到table2,并根据其他应用程序发生在table1上的更改将数据插入到table2中。

看起来我错过了一些部分。

import sqlite3

conn = sqlite3.connect("/home/sid/.Skype/testmasterut/main.db")
cursor = conn.cursor()

createLogTableSql = """create table IF NOT EXISTS sid_log as select id as "s_id",author as "s_author",timestamp as "s_timestamp",edited_by as "s_editedby",edited_timestamp as "s_edited_timestamp",body_xml as "s_body_xml" from Messages"""

cursor.execute(createLogTableSql)
conn.commit()
print "Table to save the old messages has been created"

selectLog = """ select * from sid_log """
original_table = cursor.execute(selectLog)

cursor2 = conn.cursor()
cursor3 = conn.cursor()
cursor4 = conn.cursor()

InsertTest = """ insert or ignore into sid_log (s_id,s_author,s_timestamp,s_editedby,s_edited_timestamp,s_body_xml)
select id,author,timestamp,edited_by,edited_timestamp,body_xml from Messages where id not in (select s_id from sid_log where s_id = id) and edited_by is NULL and edited_timestamp is NULL
"""

EditedTest = """ select * from Messages where id in (select s_id from sid_log where s_id = id) and edited_by is not NULL and edited_timestamp is not NULL"""
conn.close()

while True:
    conn2 = sqlite3.connect("/home/sid/.Skype/testmasterut/main.db",timeout=3)
    conn2.execute(InsertTest)

    print "Total number of rows changed:", conn.total_changes
    EditedTest2 = """ select * from Messages where id in (select s_id from sid_log where s_id = id) and edited_by is not NULL and edited_timestamp is not NULL"""
    edited_list = conn2.execute(EditedTest2)
    conn2.commit()
    conn2.close()
    # for row in edited_list:
    #   queryString = "SELECT * FROM sid_log WHERE s_id IN (%s)" % str(row[0])
    #   original_message = conn.execute(queryString)
    #   for org_row in original_message:
    #       print "Message edited from", org_row[5], "to", row[5]

修改 以下是追溯

Traceback (most recent call last):
  File "try2.py", line 28, in <module>
    conn2.execute(InsertTest)
sqlite3.OperationalError: database is locked

4 个答案:

答案 0 :(得分:6)

我不确定这是否会对任何人有所帮助,但我找到了解决我自己的锁定数据库问题的方法。

我使用PyCharm并发现我正在处理的脚本的几个实例都在运行。这通常是由于我正在测试的代码中的错误,但它保持活动状态(因此与db的连接仍处于活动状态)。关闭那些(停止所有过程)并再试一次 - 它每次都适合我!

如果有人知道在一段时间后让它超时的方法,请评论此解决方案。我试过cur.execute("PRAGMA busy_timeout = 30000")(从另一个线程中找到类似的问题),但似乎没有做任何事情。

答案 1 :(得分:4)

&#34;数据库被锁定&#34;表示某些其他连接具有活动连接。

使用PRAGMA busy_timeout等待一段时间让其他交易完成:

conn.execute("PRAGMA busy_timeout = 30000")   # 30 s

但是,如果其他应用程序故意保持打开的事务以保持数据库锁定,那么您无能为力。

答案 2 :(得分:0)

cursor2 = conn.cursor()
"""EDIT YOUR DATABASE USING CODE AND CLOSE THE CONNECTION"""
connection.close()

cursor3 = conn.cursor()
"""EDIT YOUR DATABASE USING CODE AND CLOSE THE CONNECTION"""
connection.close()

cursor4 = conn.cursor()
"""EDIT YOUR DATABASE USING CODE AND CLOSE THE CONNECTION"""
connection.close()

我认为您必须关闭已打开的连接,可能是错误是因为您已打开多个连接。

#include <stdio.h> 
#include <limits.h>
#define LARGEST(x,y) ( (x) > (y) ? (x) : (y) )
int main()
{
    int a = INT_MIN;
    int i = 0;
    for(i=0; i<5; i++)
    {
        int x = 0;
        printf("Enter the value of X:\n");
        scanf("%d", &x);
        a = LARGEST(x, a);
    }    
    printf("%d", a);
}

答案 3 :(得分:0)

我遇到了同样的问题,但是当我使用以下内容关闭并发连接时,它已得到解决。

conn.close()

所以,如果你的程序是这样开始的:

import sqlite3

conn = sqlite3.connect('pg_example.db', timeout=10)
c = conn.cursor()

确保在每个SQL语句之后包含conn.close()

t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
conn.commit()
conn.close() #This is the one you need