python - 参数化插入Python pymysql
问题描述
我有以下代码块
def write_metadata(self):
try:
with self.sql_writer.cursor as cursor:
for data in self.input_data:
sql = ("""INSERT INTO Sample (SampleName, TransferStatus, ClientSid, ClientSubjectId, ClientName, ProjectId, ProjectName, ExecutionId, Deleted)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s""")
package_data = (data.sample_name, 0, 'client_sid', "Testing", data.client_name, data.project_id, data.project_name, '12345', '0')
cursor.execute(sql, package_data)
self.sql_writer.conn.commit()
finally:
self.sql_writer.conn.close()
我得到的错误是
"errorMessage": "(1064, u\"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1\")"
我的sql参数化有问题吗?问题是因为某些值是数字而 %s 是字符串吗?问题是因为某些值是无吗?
谢谢。
解决方案
您)
在VALUES()
.
您还错误地使用了多行字符串。尝试这个:
sql = """INSERT INTO Sample (SampleName, TransferStatus, ClientSid, ClientSubjectId, ClientName, ProjectId, ProjectName, ExecutionId, Deleted)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"""
推荐阅读
- apache-kafka - Kafka流中的映射操作后双引号中的字符串
- android - 让 TextView 自动删除旧文本?
- c# - 控制台中的行没有填满可用空间
- c# - 如何通过值找到json令牌然后删除令牌
- go - 找不到 Go 包
- javascript - 如何在 JavaScript 中获取和设置 Cosmos Db 延续令牌
- webgl - GPU 是否将创建的纹理与 CPU 交换?
- javascript - 如何正确使用 AJAX + pushState 生成 URL?
- javascript - 在 thymeleaf th:text 模板上调用 javascript 函数
- c# - 如何在 c#.net2 中使用 selenium webdriver