Python Mysql issue with %s for the table -
question
why %s escape sequence not work in python script mysql package?
background , code
i have issue following line:
cursor.execute("""insert `%s` (date, counter_in, counter_out, interface_name) values (current_timestamp, %s, %s, %s)""", (equipment, in_octets, out_octets, interface)) i following error message :
traceback (most recent call last): file "snmp_query.py", line 41, in <module> cursor.execute("""insert `%s` (date, counter_in, counter_out, interface_name) values (current_timestamp, %s, %s, %s)""", (equipment, in_octets, out_octets, interface)) file "/usr/lib/pymodules/python2.6/mysqldb/cursors.py", line 166, in execute self.errorhandler(self, exc, value) file "/usr/lib/pymodules/python2.6/mysqldb/connections.py", line 35, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.programmingerror: (1146, "table 'sipartech.'itx6-f10-1'' doesn't exist") i have double checked , table itx6-f10-1 , indeed exist.
one mistake can notice in insert query write date column name without (`) symbol, date mysql date type: keyword. so, in query :-
cursor.execute("""insert `%s` (date, counter_in, ^ should
cursor.execute("""insert `%s` (`date`, `counter_in`, ^ added (`) second, couldn't understand why mysql:1146 error? happen when database files missing.
can notice %s working how find database name equipment python variable in code.
but why getting:
'sipartech.'itx6-f10-1'' ^ ^ ' of-course can't data base name , may reason mysql error:1146 , should get:
'sipartech.itx6-f10-1' check code , query.
also if have doubt %s, can use string.formate() function instead of %s. like:
""" insert {0} ( `date`, `counter_in`, `counter_out`, `interface_name`) values (current_timestamp, {1}, {2}, {3}) """.formate(equipment, in_octets, out_octets, interface)) also, remember if in_octets, out_octets, interface not integers put ' around each braces {} in query string.
Comments
Post a Comment