1

I am trying to update the data in a row of MySQL which this line of code is throwing this error.

for k in range(city_count):
    cur.execute("UPDATE hqstock SET citylastprice = '%s' WHERE id = '%s'"%(CITYPRICE[k],   tID[k]))

The error that was returned:

File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 201, in execute
    self.errorhandler(self, exc, value)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
InterfaceError: (0, '')

This is my table structure.

+--------------------+------------------+------+-----+---------+-------+
| Field              | Type             | Null | Key | Default | Extra |
+--------------------+------------------+------+-----+---------+-------+
| id                 |int(11) unsigned  | NO   | PRI | NULL    |       |
| barcode            | char(15)         | YES  |     | NULL    |       |
| citycurstock       | smallint(6)      | YES  |     | NULL    |       |
| citylastprice      | demical(4,2)     | YES  |     | NULL    |       |
| city               | varchar(60)      | YES  |     | NULL    |       |
+--------------------+------------------+------+-----+---------+-------+

Anybody knows what is wrong with my query statement? or why doesn't this work?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2828757
  • 15
  • 1
  • 5
  • possible duplicate of [mysqldb interfaceError](http://stackoverflow.com/questions/5704590/mysqldb-interfaceerror) and [InterfaceError (0, '')](http://stackoverflow.com/q/6650940) – Martijn Pieters Oct 25 '13 at 16:53
  • 3
    Note: don't use string interpolation; use SQL parameters instead: `cur.execute("UPDATE hqstock SET citylastprice = %s WHERE id = %s", (CITYPRICE[k], tID[k]))`; this leaves quoting to the database adapter (safer and more convenient). – Martijn Pieters Oct 25 '13 at 16:54

1 Answers1

1

try this :

for k in range(city_count):
    cur.execute("UPDATE hqstock SET citylastprice = '%s' WHERE id = '%s'"% (CITYPRICE[k],   tID[k]))
    cur.commit()
mrlouhibi
  • 121
  • 4