使用twisted.enterprise.adbapi从简单的SELECT获取数据

use*_*249 2 mysql twisted

我能够使用以下方式执行mySQL数据插入,

from twisted.enterprise.adbapi import ConnectionPool
.
.
self.factory.pool.runOperation ('insert into table ....')
Run Code Online (Sandbox Code Playgroud)

但是,不知何故无法弄清楚如何从adbapi调用mySQL进行简单的选择,如下所示,

self.factory.pool.runOperation('SELECT id FROM table WHERE name = (%s)',customer)
Run Code Online (Sandbox Code Playgroud)

如何从此partilcar调用中检索id值?我用普通的python工作得很好,但不知何故真的用扭曲的框架模糊了.

谢谢.

Jea*_*one 8

runOperation不适用于SELECT语句.它适用于不产生行的语句,例如INSERTDELETE.

生成行的语句受支持runQuery.例如:

pool = ...
d = pool.runQuery("SELECT id FROM table WHERE name = (%s)", (customer,))
def gotRows(rows):
    print 'The user id is', rows
def queryError(reason):
    print 'Problem with the query:', reason
d.addCallbacks(gotRows, queryError)
Run Code Online (Sandbox Code Playgroud)

在这个例子中,d是一个实例Deferred.如果您之前没有遇到过Deferreds,那么您肯定想了解它们:http://twistedmatrix.com/documents/current/core/howto/defer.html