SQLite3请求挂起扭曲

时间:2015-09-29 22:28:53

标签: python sqlite twisted

我正在尝试编写一个脚本来自动更新数据库的架构。但是,由于某种原因,我在adbapi.ConnectionPool上发出的第二个请求中挂起了挂起。这是代码:

update.py

import os
import glob
import imp

from twisted.internet import reactor
from twisted.enterprise import adbapi
from twisted.internet import defer

@defer.inlineCallbacks
def get_schema_version(conn):
    schema_exists = yield conn.runQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='schema_meta';")
    defer.returnValue(0)

def add_schema_files(schemas):
    # Finds and imports all schema_*.py files into the list
    module_files = glob.glob(os.path.dirname(os.path.abspath(__file__)) + "/schema_*.py")
    for mod in module_files:
        module_name = os.path.basename(os.path.splitext(mod)[0])
        newmod = imp.load_source('%s'%module_name, mod)
        schemas.append( (module_name, newmod) )

@defer.inlineCallbacks
def update_schema(conn):
    # Update the database schema to the latest version
    schema_version = yield get_schema_version(conn)
    print "At schema version %d" % schema_version
    schemas = []
    add_schema_files(schemas)
    schemas = sorted(schemas, key=lambda tup: tup[0])
    for i in range(schema_version, len(schemas)):
        # schemas[0] is v1, schemas[1] is v2, etc
        print "Updating to version %d" % (i+1)
        yield schemas[i][1].update(conn)

if __name__ == '__main__':
    conn = adbapi.ConnectionPool("sqlite3", "data.db", check_same_thread=False)
    d = update_schema(conn)
    d.addCallback(exit)
    reactor.run()

schema_1.py

from twisted.internet import defer

update_query = """
CREATE TABLE schema_meta (
version INT NOT NULL
);
INSERT INTO schema_meta (version) VALUES (1);
"""

@defer.inlineCallbacks
def update(conn):
    yield conn.runQuery(update_query)

它挂在schema_1.py中的yield conn.runQuery(update_query)

此外,当我中断脚本时,我收到以下sqlite错误:

sqlite3.Warning: You can only execute one statement at a time.

2 个答案:

答案 0 :(得分:1)

SQLite允许您一次运行一个写入查询。您的代码是异步的,可以在第一个代码完成之前启动新的查询。

要处理此问题,您必须序列化查询或使用数据库服务器而不是SQLite。

答案 1 :(得分:0)

事实证明,问题不在于扭曲,而在于SQLite查询。您一次只能执行一个查询,因此我更新了schema_1.py并且工作正常。

schema_1.py

from twisted.internet import defer

update_query1 = """
CREATE TABLE schema_meta (
version INT NOT NULL
);
"""

update_query2 = """
INSERT INTO schema_meta (version) VALUES (1);
"""

@defer.inlineCallbacks
def update(dbpool):
    yield dbpool.runQuery(update_query1)
    yield dbpool.runQuery(update_query2)