我尝试使用以下代码:
from twisted.enterprise import adbapi
dbpool = adbapi.ConnectionPool(
"MySQLdb",
db='test_db',
port='3306',
user='tester',
passwd='some_pass',
host='localhost',
cp_reconnect=True
)
dbpool.runQuery("INSERT INTO `htp_test` VALUES(NULL, 25, 'test')")
但是数据没有插入mysql,也没有显示错误。要连接的数据很好。
答案 0 :(得分:4)
你需要启动反应堆。 adbapi
中的“a”用于“异步”,就像扭曲的其他所有内容一样。当您致电ConnectionPool.runQuery()
时,您已要求在后台执行某些工作,完成后,请在runQuery
返回的延期中提供该操作的结果。
但是为了扭曲“做”任何事情,你有义务开始它的事件循环。在最简单的情况下,您可以:
from twisted.internet import reactor
from twisted.enterprise import adbapi
def got_result(value):
# do something, value won't be interesting on insert statements, though
print "Horray"
# since this is all we want to do, stop the reactor
reactor.stop()
d = dbpool.runQuery("INSERT INTO `htp_test` VALUES(NULL, 25, 'test')")
d.addCallback(got_result)
reactor.run()