0

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
3
  • Read the docs, especially about the params parameter: dev.mysql.com/doc/connector-python/en/… Commented Feb 18, 2024 at 18:21
  • I used the link to modify to: updatedata = ( "UPDATE enviromental SET temp = %s WHERE room = %s" "VALUES (%s, %s)" ) data = (hot_water_temp, 'Hot Water Tank A') mycursor.execute(updatedata, data) But the error now is now that there are not enough parameters for the SQL statement Commented Feb 18, 2024 at 19:09
  • 1
    UPDATE does not take a VALUES clause. The number of parameters in your data list must be the same as the number of %s placeholders in your query. Commented Feb 18, 2024 at 20:57

1 Answer 1

0

Finally puzzled this out, but the linked documentation was of little use, as like most other posted information, it references the INSERT, rather than UPDATE. It was essential to use two variables, looking like this:

sql_update_query = """UPDATE enviromental SET temp = %s WHERE room = %s"""

input_data = (hot_water_temp, 'Hot Water Tank A')

mycursor.execute(sql_update_query, input_data)

I sill don't quite understand what is going on, but this fragment works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.