python - Python 格式化字符串文字返回“SyntaxError:无效语法”
问题描述
我正在使用 python cx_oracle ,当我尝试将变量插入 sql 时:
cursor.executemany(f"INSERT INTO table ({columns}) VALUES ({placeholders })",t)
它返回:
File "eula_db.py", line 77
cursor.executemany(f"INSERT INTO table ({columns}) VALUES ({placeholders })",t)
^
SyntaxError: invalid syntax
解决方案
使用 Python 3.6
t.py 包含:
columns = 'abc'
placeholders = ':b'
s = f"INSERT INTO EULA_STG_DATA ({columns}) VALUES ({placeholders })"
print(s)
行为取决于 Python 版本:
$ python --version
Python 2.7.16
$ python t.py
File "/Users/cjones/py/so15.py", line 3
s = f"INSERT INTO EULA_STG_DATA ({columns}) VALUES ({placeholders })"
^
SyntaxError: invalid syntax
使用较新的 Python:
$ python --version
Python 3.9.1
$ python t.py
INSERT INTO EULA_STG_DATA (abc) VALUES (:b)
推荐阅读
- mysql - 如何从满足元数据表的两个或多个条件的表中获取数据?
- php - 由 Cron 调用函数在其中做很多事情的解决方案?
- python - 使用 sklearn 将多行作为单个批次传递以进行预测
- arrays - 用奇怪的分类器找到 9 个具有相同“颜色”的元素
- python - 是什么导致了这个错误(ValueError: could not convert string to float: 'High')?
- android - 如何使用Android中的导航控制器组件检测片段是来自前一个片段还是来自之后的片段?
- firebase - firebase_auth 和 cloud_firestore 颤动时出错(尝试了其他解决方案)
- java - 在 Spring Cloud 应用程序中实现重试
- laravel - 为 Laravel API 实施全局应用程序设置?
- javascript - 为什么文本框不接受任何输入,我想要表单提交操作中的所有 formData?