I am trying to update (not insert) a python variable into a Mysql database. Searches show me many examples of the UPDATE being set to a constant,and this works for me: fragment
mycursor.execute("UPDATE enviromental SET temp = 10 WHERE room = 'Hot Water Tank A'")
mydb.commit()
works well,
but,
print (hot_water_temp)
mycursor.execute("UPDATE enviromental SET temp = hot_water_temp WHERE room = 'Hot Water Tank A'")
mydb.commit()
gives me:
Traceback (most recent call last): File "/srv/nfs/mysqlconnector.py", line 20, in mycursor.execute("UPDATE enviromental SET temp = hot_water_temp WHERE room = 'Hot Water Tank A'") File "/home/paul/.local/lib/python3.11/site-packages/mysql/connector/cursor.py", line 551, in execute self._handle_result(self._connection.cmd_query(stmt)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/paul/.local/lib/python3.11/site-packages/mysql/connector/connection.py", line 490, in cmd_query result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/paul/.local/lib/python3.11/site-packages/mysql/connector/connection.py", line 395, in _handle_result raise errors.get_exception(packet) mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'hot_water_temp' in 'field list'
Is my python variable just not seen inside the SQL statement? I not, how can I get it into the statement?
This must be a pretty common thing to want to do, but none of my searches gave me even a hint of how this should be done
Regards
paramsparameter: dev.mysql.com/doc/connector-python/en/…UPDATEdoes not take aVALUESclause. The number of parameters in yourdatalist must be the same as the number of%splaceholders in your query.