【发布时间】:2014-04-03 10:10:28
【问题描述】:
这是我尝试的代码 sn-p
query = tbl_scores.select().limit( bindparam('lim') )
但是,我收到以下错误。
TypeError: int() argument must be a string or a number, not 'BindParameter'
谁能提供一个使用 bindparam 进行 LIMIT/OFFSET 的示例?
Python 2.7.5、SQLAlchemy 0.8.4
假设一个 webapi 返回顶级玩家和他们的排名。使用这样的绑定参数构造一个查询,然后将其存储在线程本地位置,以便请求可以共享相同的预编译查询。
# TypeError occurs here
a_thread_local_place.query = join(tbl_scores,
tbl_master_player,
tbl_scores.c.uid == tbl_master_player
).\
select().limit(bindparam('lim'))
每次 webapi 处理请求时,我想使用预编译查询执行这样的查询。
result = engine.connect().execute(
a_thread_local_place.query,
lim = 10,
)
【问题讨论】:
-
试试这个
query = tbl_scores.select().limit(10)。 Refer Here -
谢谢赛义德,我知道查询工作正常。但是,出于性能原因,我想将其设置为 bindparam'd,因为我的实际查询对于许多连接表来说更加复杂。
-
您能告诉我们您是如何执行该查询的吗?就像示例执行代码一样。
-
好的,赛义德。我编辑了问题。
-
你注意到一件事了吗?
bindparam回复<object BindParameter>。但是limit正是想要type str或type int。你能把bindparam对象更改为str或int。这可以解决你的问题。
标签: python sqlalchemy