views:

28

answers:

2

I am attempting to execute the following query via the mysqldb module in python:

for i in self.p.parameter_type: cursor.execute("""UPDATE parameters SET %s = %s WHERE parameter_set_name = %s""" % (i, float(getattr(self.p, i)), self.list_box_parameter.GetStringSelection()))

I keep getting the error: "Unknown column 'M1' in 'where clause'". I want to update columns i with the value getattr(self.p, i), but only in rows that have the column parameter_set_name equal to self.list_box_parameter.GetStringSelection(). The error suggests that my query is looking for columns by the name 'M1' in the WHERE clause. Why is the above query incorrect and how can I correct it?

A: 

It looks like query is formed with wrong syntax. Could you display string parameter of cursor.execute?

For example, putting in actual strings instead of %s, the above query would like like:UPDATE tablename SET columnname = "something" WHERE another_column_name = "something_else"The error is suggesting that my where clause is searching for the column "something else" rather than the column another_column_name with the value "something_else"
curious
I've tried the query with real strings in MySQL directly and that seemed to work. I believe the trouble is with the syntax around %s, or the way I am using cursor.execute ...
curious
I mean that you should check EXACT (calculated) parameter of execute in debugger.
+1  A: 

i see now, i think you need to enclose parameter_set_name = %s in quotes such as:

parameter_set_name = "%s"

otherwise it's trying to acces column M1

so:

cursor.execute("""UPDATE parameters SET %s = %s WHERE parameter_set_name = \"%s\" """ % (i, float(getattr(self.p, i)), self.list_box_parameter.GetStringSelection()))
pulegium
That worked!! Thank you :) Why do I need the quotes for the third case of %s but not the 2nd case?
curious
i'm guessing mysql sees a float number which cannot be column name so just works out that it's a value rather than a name
pulegium
Cool -- thank you :)
curious