2013-10-25 79 views
1

我正在嘗試更新一行MySQL中的數據,這行代碼引發了此錯誤。MySQL Python更新行

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

被返回的錯誤:

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, '') 

這是我的表結構。

+--------------------+------------------+------+-----+---------+-------+ 
| 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 |  | 
+--------------------+------------------+------+-----+---------+-------+ 

有人知道我的查詢語句有什麼問題嗎?或者爲什麼不工作?

+0

可能重複的[MySQLdb的interfaceError](http://stackoverflow.com/questions/5704590/mysqldb-interfaceerror)和[InterfaceError(0, '')](HTTP: //stackoverflow.com/q/6650940) –

+3

注意:不要使用字符串插值;請使用SQL參數:'cur.execute(「UPDATE hqstock SET citylastprice =%s WHERE id =%s」,(CITYPRICE [k],tID [k]))';這留下引用數據庫適配器(更安全,更方便)。 –

回答

1

嘗試:

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

工程就像一個魅力。這有助於我遇到的第二個問題。謝謝 – user2828757