参数插入Python pymysql

时间:2018-12-05 20:51:39

标签: 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是字符串,是问题所在吗? 是因为某些值是None造成问题了吗?

谢谢。

1 个答案:

答案 0 :(得分: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)"""