pythonpyodbcpypyodbc

Update query using python odbc cursor - Not working


I have this query executed with the following snippet:

cursor2.execute("UPDATE datatable SET data1 = ?, data2 = ?, data3 = ? WHERE id = ?",[d1,d2,d3,i])
print("affected rows = {}".format(cursor2.rowcount))

Affected rows returns 1 but row in database was not updated.

Printing the d1,d2,d3 is fine only the update in the database doesn't work.

Is there something wrong with the function?

Additional:

I tried this way:

sql_update_query = """UPDATE datatable SET data1 = %s, data2 = %s, data3 = %s WHERE id = %s"""
inputData = (d1,d2,d3,i)
cursor2.execute(sql_update_query, inputData)

But it retuns this error:

('HY000', 'The SQL contains 0 parameter markers, but 4 parameters were supplied')

Thanks for the help.


Solution

  • Looks like you are missing .commit()

    Ex:

    cursor2.execute("UPDATE datatable SET data1 = ?, data2 = ?, data3 = ? WHERE id = ?",(d1,d2,d3,i))
    cursor2.commit()