2017-05-12 5 views
0

私はTwistedのtwisted.enterprise.adbapi.ConnectionPoolクラスを使用してメモリ内のsqlite3データベースを作成しようとしています。私たちは永続ストレージ、例えば上のファイルへのパスで:memory:を交換する場合は、物事は奇妙な取得sqlite3.OperationalError: no such table: ajxp_indexCREATEクエリの実行後にsqlite3のメモリ内テーブルが存在しないのはなぜですか?

がある:上記のスクリプトを実行

#! /usr/bin/env python 
from twisted.internet import task 
from twisted.internet import defer 
from twisted.enterprise.adbapi import ConnectionPool 

sql_init = (
    "CREATE TABLE ajxp_index (node_id INTEGER PRIMARY KEY AUTOINCREMENT);", 
    "INSERT INTO ajxp_index (node_id) VALUES (9001);", 
) 


@task.react 
@defer.inlineCallbacks 
def main(reactor): 
    cp = ConnectionPool("sqlite3", ":memory:", check_same_thread=False) 
    for sql in sql_init: 
     print(sql) 
     yield cp.runQuery(sql) 

次のエラーを生成します。次のように私のテスト・ケースは次のようになります。 /tmp/foo.sqliteこの条件の下では、スクリプトは期待通りに実行されます。与えるもの

import sqlite3 

conn = sqlite3.connect(":memory:") 
for sql in sql_init: # same commands as in above example 
    conn.execute(sql) 

さらに、予想通りの標準ライブラリの実行にsqlite3モジュールを使用して、同じSQLクエリを実行していますか?これはTwistedのバグですか?何か間違っていますか?

EDIT

:notorious.noの提案を1として

は、私がcp.runInteractionを使用するように私の例を更新しましたが、結果は同じまま:

@task.react 
@defer.inlineCallbacks 
def main(reactor): 
    cp = ConnectionPool("sqlite3", ":memory:", check_same_thread=False) 
    for sql in sql_init: 
     print(sql) 
     yield cp.runInteraction(lambda cursor: cursor.execute(sql)) 

EDIT 2

さて、この動作しているようです:

def _interact(cursor, script): 
    cursor.executescript(script) 


@task.react 
@defer.inlineCallbacks 
def main(reactor): 
    cp = ConnectionPool("sqlite3", ":memory:", check_same_thread=False) 
    yield cp.runInteraction(_interact, "\n".join(sql_init)) 

答えて

関連する問題