Search code examples
pythonmysqlfetchall

python MySQL Fetchone / Fetchall update


I have tried everything for the past 48 hours and have not figured out where I am going wrong.

The cursor.fetchone() works as shown:

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

but the cursor.fetchall() fails and says:

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


Solution

  • I think what's happening here is that you are passing a tuple where string is expected. You said in a comment that i is ('AAL.L',), i think cursor.execute is formatting it as a string. Try this:

    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]))