I currently have a MySQL table that looks like:
mysql> SELECT * FROM settings;
+--------------+-------+
| name | value |
+--------------+-------+
| connected | 0 |
+--------------+-------+
and I'm trying to update the value with a timestamp for connected through Python with this code:
db1 = MySQLdb.connect(host="localhost", user="user", passwd="password", db="database")
cursor1 = db1.cursor()
cursor1.execute("UPDATE settings SET value = 'ts' WHERE name = connected")
db1.commit()
cursor1.close()
This returns to me:
cursor1.execute("""UPDATE settings SET value = 'ts' WHERE name = connected""")
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue _mysql_exceptions.OperationalError: (1054, "Unknown column 'connected' in 'where clause'")
and I just can't figure it out. Any help would be really appreciated.
1 Answer 1
1054, "Unknown column 'connected' in 'where clause'
This error clearly states that connected is consider as a column name.
But here connected is string value that want to match with the columnname name.
So you need to place a single quote around the connected string to solve the issue:
cursor1.execute("UPDATE settings SET value = 'ts' WHERE name = 'connected'")
namecolumn to a column calledconnected. If you want to compare thenamecolumn to the literal string "connected", then you meanWHERE name='connected'