Python& SnakeSQL - 引发lock.LockError('Lock不再有效。')ERROR

时间:2016-04-02 21:13:26

标签: sql python-2.7

我正在尝试运行一个python脚本(createdb.py),该脚本具有来自我的主python脚本(app.py)的数据库操作,但出现以下错误。

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\web\application.py", line 236, in process
    return self.handle()
  File "C:\Python27\lib\site-packages\web\application.py", line 227, in handle
    return self._delegate(fn, self.fvars, args)
  File "C:\Python27\lib\site-packages\web\application.py", line 409, in _delegate
    return handle_class(cls)
  File "C:\Python27\lib\site-packages\web\application.py", line 384, in handle_class
    return tocall(*args)
  File "D:\Python\virtualenvs\new4\textweb\bin\app.py", line 16, in GET
    createdb.createTables()
  File "D:\Python\virtualenvs\new4\textweb\bin\createdb.py", line 9, in createTables
    cursor.execute("CREATE TABLE table (dateColumn Date, numberColumn Integer)")
  File "D:\Python\virtualenvs\new4\textweb\bin\SnakeSQL\driver\base.py", line 1548, in execute
    self.info = self.connection._create(parsedSQL['table'], parsedSQL['columns'], parameters)
  File "D:\Python\virtualenvs\new4\textweb\bin\SnakeSQL\driver\base.py", line 993, in _create
    self._insertRowInColTypes(table)
  File "D:\Python\virtualenvs\new4\textweb\bin\SnakeSQL\driver\base.py", line 632, in _insertRowInColTypes
    ], types= ['String','String','String','Bool','Bool','Bool','Text','Text','Integer']
  File "D:\Python\virtualenvs\new4\textweb\bin\SnakeSQL\driver\dbm.py", line 61, in _insertRow
    self.tables[table].file[str(primaryKey)] = str(values)
  File "D:\Python\virtualenvs\new4\textweb\bin\SnakeSQL\external\lockdbm.py", line 50, in __setitem__
    raise lock.LockError('Lock no longer valid.')
LockError: Lock no longer valid.

这是我的createdb.py代码;

import SnakeSQL

connection = SnakeSQL.connect(database='test', autoCreate=True)
connection = SnakeSQL.connect(database='test')

cursor = connection.cursor()

def createTables():
    cursor.execute("CREATE TABLE table (dateColumn Date, numberColumn Integer)")
    cursor.execute("INSERT INTO table (dateColumn, numberColumn) VALUES ('2003-11-8', 3)")
    cursor.execute("INSERT INTO table (dateColumn, numberColumn) VALUES ('2004-11-8', 4)")
    cursor.execute("INSERT INTO table (dateColumn, numberColumn) VALUES ('2005-11-8', 5)")
    cursor.execute("INSERT INTO table (dateColumn, numberColumn) VALUES ('2006-11-8', 6)")


def select():
    selectResult = cursor.execute("SELECT dateColumn FROM table WHERE numberColumn = 3")
    return selectResult

if __name__ == "__main__":
    createTables()

这是我的app.py代码;

import web
import SnakeSQL
import createdb

render = web.template.render('templates/')

connection = SnakeSQL.connect(database='test')
cursor = connection.cursor()

urls = (
    '/', 'index'
)

class index:
    def GET(self):
        createdb.createTables()
        result = createdb.select()
        return render.index(result)


if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()   

我无法找出我遇到此错误的原因。你能否分享一下你解决这个问题的知识?

1 个答案:

答案 0 :(得分:1)

首先,SnakeSQL docs似乎来自2004年,实际代码最后一次更新于2009年,作者声明该项目已不再维护。您可能需要考虑使用仍然积极维护的内容。

docs也提到:

  

理论上,访问数据库的其中一个进程可能陷入无限循环而不释放数据库上的锁,以允许其他用户访问它。经过2秒钟后,如果数据库上当前锁定的进程无法访问它,则锁定将被释放,另一个进程可以获得锁定。第一个进程本身必须等待获取锁。

看看你的追溯,我会做一个有根据的猜测,因为你把光标放在模块级别(这可能你不想这样做),它在模块首次导入时创建了光标,然后当你的程序实际运行createTables函数时,已超过2秒,并且它已放弃锁定。

尝试移动线条以在方法中创建光标:

def createTables():
    cursor = connection.cursor()
    cursor.execute("CREATE TABLE table (dateColumn Date, numberColumn Integer)")
    cursor.execute("INSERT INTO table (dateColumn, numberColumn) VALUES ('2003-11-8', 3)")
    cursor.execute("INSERT INTO table (dateColumn, numberColumn) VALUES ('2004-11-8', 4)")
    cursor.execute("INSERT INTO table (dateColumn, numberColumn) VALUES ('2005-11-8', 5)")
    cursor.execute("INSERT INTO table (dateColumn, numberColumn) VALUES ('2006-11-8', 6)")


def select():
    cursor = connection.cursor()
    selectResult = cursor.execute("SELECT dateColumn FROM table WHERE numberColumn = 3")
    return selectResult

(并在app.py代码中执行相同操作)。