如何将ASC DESC作为Python Psycopg2 Raw SQL执行中的参数传递?
我有一个示例查询模板可以接受要执行的动态参数:
ps_conn = psycopg2.connect(...)
ps_cursor = ps_conn.cursor()
ps_cursor.execute('''
SELECT *
FROM "posts"
) ORDER BY "posts"."rate" %s, "posts"."date_created" ASC LIMIT 1
''', ["DESC"])
正如您所看到的,我想动态地传递到结果,但是上面的代码继续抛出错误
架构“帖子”不存在 第11行:)按“帖子”订单。“速率”'desc',
它以'desc'的方式将desc传递给SQL。
有任何意见如何执行此功能,以便我可以动态地传递订购类型?
i have a sample query template to accept dynamic parameters to execute:
ps_conn = psycopg2.connect(...)
ps_cursor = ps_conn.cursor()
ps_cursor.execute('''
SELECT *
FROM "posts"
) ORDER BY "posts"."rate" %s, "posts"."date_created" ASC LIMIT 1
''', ["DESC"])
as you can see i wanna pass DESC dynamically to orderby the results , but the above code keep throwing the error InvalidSchemaName
schema "posts" does not exist
LINE 11:) ORDER BY "posts"."rate" 'DESC',
it passes the DESC as 'DESC' to the sql .
any opinion how to perform this functionality so i can pass ordering type dynamically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这样更新您的查询,然后尝试:
Update your query like this and try:
相反,我将创建一个单独的变量和代码如下:
I would instead create a separate variable and code as follows: