python - 包含 SQL 查询的字符串中的变量 - Python
问题描述
我有一个字符串设置如下:
sql_string = ( """ select * from schema.table where column1 like '%object%' and column2 = '%s' """)%(x)
不幸的是,当我运行脚本时,出现以下错误:
TypeError: not enough arguments for format string
有什么方法可以让我的查询字符串中有一个变量吗?我怀疑查询的 column1 部分中的百分号是导致问题的原因。
谢谢!
解决方案
您可以使用其他 % 转义 %:
sql_string = ( """ select * from schema.table where column1 like '%%object%%' and column2 = '%s' """)%(x)
推荐阅读
- android - 我可以为视图和附加到/具有约束关系的所有其他视图设置动画吗?
- python - 要合并的不同长度的 Python 时间序列数据集
- javascript - 仅在单击按钮时显示一个类
- android - Ionic 3 提示在 Android 上的键盘上输入空格
- c++ - C ++在编译时具有计算全局变量的功能
- node.js - Nodejs从promise bluebird返回值请求到变量
- java - 具有多个按钮和不同 ID 的 CustomView 侦听器(回调)
- erlang - 首先放置最可能匹配的功能头是否有任何好处?
- css - 如何在相同大小的轮播中制作图像?
- javascript - 通过单击激活输入字段并在离开后清除选择字段