gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:14:28 24 4
gpt4 key购买 nike

我可以使用以下方法插入 mySQL 数据,

from twisted.enterprise.adbapi import ConnectionPool
.
.
self.factory.pool.runOperation ('insert into table ....')

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

self.factory.pool.runOperation('SELECT id FROM table WHERE name = (%s)',customer)

如何从此 partilcar 调用中检索 id 值?我在普通 python 上工作正常,但不知何故真的对扭曲的框架感到困惑。

谢谢。

最佳答案

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)

在这个例子中,dDeferred 的一个实例。如果您之前没有遇到过 Deferred,您肯定想阅读有关它们的信息:http://twistedmatrix.com/documents/current/core/howto/defer.html

关于mysql - 使用 twisted.enterprise.adbapi 从简单的 SELECT 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8578531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com