簡體   English   中英

使用pymysql上傳時如何解決pymysql.err.programmingError

[英]How to solve pymysql.err.programmingError during upload using pymysql

我想創建一個 dataframe 並將其更新為 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 構造,這樣您就不需要兩次數據,因為您的原始值有雙倍數,但只發送一次

$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)")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM