【问题标题】:Parametrize insert Python pymysql参数化插入Python pymysql
【发布时间】:2018-12-06 05:03:51
【问题描述】:

我有以下代码块

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 是字符串吗? 问题是因为某些值是 None 吗?

谢谢。

【问题讨论】:

  • 现在测试@roganjosh。谢谢。
  • ProgrammingError: (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 2"
  • 您缺少右括号。

标签: python pymysql


【解决方案1】:

您在VALUES() 的末尾缺少了一个右括号)

您还错误地使用了多行字符串。试试这个:

sql = """INSERT INTO Sample (SampleName, TransferStatus, ClientSid, ClientSubjectId, ClientName, ProjectId, ProjectName, ExecutionId, Deleted)
          VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"""

【讨论】:

  • 感谢您修复多字符串并添加右括号已解决问题。
猜你喜欢
  • 2014-12-07
  • 2019-01-01
  • 1970-01-01
  • 2013-04-25
  • 2018-10-14
  • 2013-04-18
  • 1970-01-01
  • 2015-04-13
  • 1970-01-01
相关资源
最近更新 更多