python MySQL Fetchone / Fetchall 更新

python MySQL Fetchone / Fetchall update

我在过去的 48 小时里尝试了所有方法,但还没弄清楚哪里出了问题。

cursor.fetchone() 的工作原理如下所示:

row = cursor.fetchone()
for i in row:
        x = '13.5m'
        cursor.execute("""UPDATE table SET market_cap =%s WHERE symbol =%s""", (x,i))

但是 cursor.fetchall() 失败并说:

"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 ')' at line 1")

我认为这里发生的事情是您传递了一个 tuple,而预期 string。你在评论中说 i('AAL.L',),我认为 cursor.execute 正在将其格式化为字符串。试试这个:

row = cursor.fetchone()
x = '13.5m' # this can be outside the iteration since it's the same value every time
for i in row:
    cursor.execute("UPDATE table SET market_cap =%s WHERE symbol =%s", (x, i[0]))