我正在尝试编写Python代码以插入新行,或者如果主键存在,则使用从变量计算的新值更新2列。
cnx = mysql.connector.connect(user='admin', password='admin', host='127.0.0.1', database='cr_database')
cur = cnx.cursor()
country_string = 'Canada'
number_string = '1234567890'
now_datetime = datetime.now()
attr1_integer = 1
attr2_integer = 10
cur.execute("INSERT INTO cr_table(cr_string, cr_date, cr_country_string, cr_attr1, cr_attr2) VALUES (%s %s %s %s %s) ON DUPLICATE KEY
UPDATE cr_attr1 = cr_attr1 + 1, cr_attr2 = cr_attr2 + VALUES(%s)",(number_string, datetime.now('%Y-%m-%d'), country_string , attr1_integer, attr2_integer, attr2_integer) )
但是我收到以下错误:
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '1 10) ON DUPLICATE KEY UPDATE cr_attr1 = cr_attr1 + 1, cr_attr2 = ' at line 1
相同的查询在MySQL编辑器中完美运行(在删除Python代码之后) 我在Python代码中做错了什么?