python - 使用python参数化的postgresql select语句
问题描述
sql="select %s,tablename from pg_table_def where tablename like (%s)"
data=("schemaname","abc",)
cur.execute(sql,data)
如果我如上所述传递一个值,则选择将其作为字符串。这不是本意。
如果我尝试
data=(schemaname,"abc",)
然后它显示错误global name 'schemaname' is not defined
。
解决方案
您不能以这种方式参数化对象名称(在本例中为列名称)。您可以改为使用字符串操作:
column = "schemaname"
sql = "select {}, tablename from pg_table_def where tablename like (%s)".format(column)
data= ("abc",)
cur.execute(sql,data)
推荐阅读
- c# - 如何修复:“在 MS SQL Server 重新启动 alipne .net core 2.2.5 容器后,使用它的容器有一个 CLOSE_WAIT tcp 连接 - dotnet 的 CPU 上升”
- java - 无法访问类 com.sun.javafx.scene.control.skin.resources.ControlResources(在模块 javafx.controls 中)
- gitlab-ci - 如何在 gitlab CI 管道期间在构建主机(不是 docker 容器)上运行脚本
- json - 使用 jq Azure DevOps Bash 任务替换 json 文件中的值会添加随机数据
- sql - 如何从当月获取前 12 个月的最后一天数据
- java - 如何在 apache camel 中正确使用直接组件?
- wcf - WCF 服务值错误不在预期范围内
- amazon-web-services - 如何使用 jenkins 构建并在 aws 上部署?
- typescript - 打字稿中的静态是什么意思?
- javascript - 如何使用 react redux 配置 mapDispatchToProps?