使用pymysql上传时如何解决pymysql.err.programmingError

How to solve pymysql.err.programmingError during upload using pymysql

我想创建一个数据框并将其更新为 mysql。 有重复键则更新,无重复键则插入。

user = 'test'
passw = '...'
host = '...'
port = '...'
database = '...'
conn = pymysql.connect(host=host,
                           port=port,
                           user=user,
                           password=passw,
                           database=database,
                           charset='utf8')
curs = conn.cursor()
data = list(dataframe.itertuples(index=False, name=None))

sql = "insert into naversbmapping(brand, startdate, enddate, cost, daycost) values (%s, %s, %s, %s, %s) on duplicate key update brand = %s, startdate = %s, enddate = %s, cost = %s, daycost = %s"
curs.executemany(sql, data)
conn.commit()
conn.close()

但是,我收到以下错误。我该如何解决?

pymysql.err.ProgrammingError: (1064, "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 '%s, startdate = %s, enddate = %s, cost = %s, daycost = %s' at line 1")
)

您使用以下 MySQL 结构,这样您就不需要两次数据,因为您的原始值数量是原来的两倍,但只发送一次 [=14] =]

$sql = "INSERT INTO naversbmapping(brand, startdate, enddate, cost, daycost) VALUES (%s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE brand = VALUES(brand), startdate = VALUES(startdate), enddate = VALUES(enddate), cost = VALUES(cost), daycost = VALUES(daycost)")