Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy Core: How can I use bindparam for LIMIT/OFFSET clause?

Here is the code snippet I attempted

query = tbl_scores.select().limit( bindparam('lim') )

However, I got the following error.

TypeError: int() argument must be a string or a number, not 'BindParameter'

Can anybody present an example of using bindparam for LIMIT/OFFSET?

Python 2.7.5, SQLAlchemy 0.8.4


Assume an webapi to return top players and their ranks. Construct an query with the bind parameter like this, then store it in a thread local place so that requests can share the same precompiled query.

# 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'))

Everytime the webapi handles a request, I would like to execute the query like this using the precompiled query.

result = engine.connect().execute(
    a_thread_local_place.query,
    lim = 10,
)
like image 296
yukinarit Avatar asked Feb 20 '26 17:02

yukinarit


1 Answers

Currently you cannot, but there's an open issue for this and a patch is in progess:

https://bitbucket.org/zzzeek/sqlalchemy/issue/3034/use-my-own-bindparam-for-querylimit

like image 137
Dobes Vandermeer Avatar answered Feb 23 '26 07:02

Dobes Vandermeer