SQLite3请求挂在扭曲

用户名

我正在尝试编写一个脚本来自动更新数据库的架构。但是,由于某种原因,我在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)

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

此外,当我破坏脚本时,会出现以下sqlite错误:

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

事实证明,问题不在于扭曲,而在于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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章