Python MySQL插入或更新变量

时间:2016-04-22 07:35:04

标签: python mysql insert sql-update

我正在从sqlite迁移到mysql并且遇到了Insert或Update语句。据我所知,mysql使用ON DUPLICATE KEY UPDATE。这是我到目前为止所得到的,但它不会更新,它会在第一个重复的条目停止。

with open('test.csv', 'rb') as csvfile:
 reader = csv.reader(csvfile, delimiter=';', quotechar='"')
 reader.next()
 for row in reader:
  row[8]=datetime.datetime.strptime(row[8],"%d.%m.%Y").strftime("%Y-%m-%d")
  cursor.execute("INSERT INTO TABLE (Umsatz,Waehrung,Kundenname,UstID,BUKey,Kundenkonto,Rechnungsnummer,Belegfeld2,Rechnungsdatum,Sachkonto,Belegtext,MwSt) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s); ON DUPLICATE KEY UPDATE", row)

db.commit()

1 个答案:

答案 0 :(得分:0)

您需要查看documentation,您的查询将无法正常工作(在ON DUPLICATE KEY UPDATE之前查看分号)。

INSERT INTO TABLE (       
  Umsatz,
  Waehrung,
  ...
) VALUES (
  %s, 
  %s,
  ...
) ON DUPLICATE KEY UPDATE
  -- here you need to specifiy what to update on duplicate key, see documentation
;