python中的Mysql语法错误

时间:2018-02-10 05:37:46

标签: python sql pymysql

我有5个MySQL列。我想用for循环将数据插入到我的列中。但我总是得到一个错误。

SQL查询:

for i in range(5):
    sql = "INSERT INTO fighters({},{}) VALUES(%s,%s) WHERE clan=%s".format("shot_" + str(i), "damage_" + str(i))
    cur.execute(sql, (shot, damage, clanname)
    conn.commit()

错误:

pymysql.err.ProgrammingError: (1064, u"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE clan='blacks'' at line 1")

1 个答案:

答案 0 :(得分:0)

您需要更新而不是插入:

sql = "UPDATE FIGHTERS SET shot_{0} = %s, damage_{1} = %s WHERE clan=%s".format("shot_" + str(i), "damage_" + str(i))
cur.execute(sql, (shot, damage, clanname,))

希望有所帮助。