2012-03-17 50 views
1

我已經使用MySQLdb創建了一個數據庫。 在數據庫中,我有名字的學生一個表列:Mysqldb更新錯誤typeerror'str'對象無法調用

id(is int), 
id_user(is int), 
f_name(is str), 
l_name(is str) 

我更新與功能更新的行。 我的代碼如下:

def UpdateUser(self,e):   
     self.checkid_user=int(self.updateid[0]) #ok there 
     self.c2name=self.name.GetValue() #this is from a textctrl in wxpython 
     self.c2lastname=self.lastname.GetValue()#this is from a textctrl in wxpython 

     ne=self.c2name.encode("utf-8")#greek word 
     fe=self.c2lastname.encode("utf-8")#greek word 

     f="'"+str(ne)+"'" 
     l="'"+str(fe)+"'" 

     print f #ok 
     print l #ok 
     db=mdb.connect(host="localhost",use_unicode="True",charset="utf8",user="root",passwd="root",db="test") 
     cursor = db.cursor() 

     sql="""SELECT id_user FROM student""" 

     try: 
      # Execute the SQL command 
      cursor.execute(sql) 
      # Commit your changes in the database 
      db.commit() 
     except: 
      # Rollback in case there is any error 
      db.rollback() 

     rows = cursor.fetchall() 

     for row in rows: 
      r=int(row[0]) 
      if r==self.checkid_user:     #ok there 
       sql2 = """UPDATE student 
       SET f_name=%s,l_name=%s 
       WHERE id_user=%s""" 

       # Execute the SQL command 
       cursor.execute(sql2(f,l,r)) 
     # Commit your changes in the database 
       db.commit() 

       db.rollback() 
# disconnect from server 
db.close() 

當我跑我會採取以下誤差函數:

 
typeerror 'str' object is not callable 

我用fl可以被調用,但一無所獲。
我需要一些幫助來解決這個問題。謝謝!

+2

郵政錯誤消息的其餘部分。它會告訴你錯誤的實際位置。 – kindall 2012-03-17 18:34:13

回答

4

可能有其他的問題,但讓我們開始這樣的:

cursor.execute(sql2(f,l,r)) 

...是錯誤的。

你需要一個逗號的參數之間:

cursor.execute(sql2, (f,l,r)) 

您的代碼看起來像一個函數調用:this_is_how_we_call_a_func() < - 注意括號!
sql2只包含一個字符串,它是 - 作爲證據將顯示 - 不可調用...

>>> dir(sql2) 
['__add__', '__class__', '__contains__', '__delattr__', ... other stuff 

>>> def my_func(): pass 
... 
>>> dir(my_func) 
['__call__', '__class__', '__closure__', '__code__', ... other stuff 
^^^^^^^^^^ 
+0

Yeeeeeeees,有時候別人必須看到代碼才能找到丟失的東西! – TLSK 2012-03-17 18:48:19

+0

每天都有這樣的事情發生在我身上。去獲得下一個錯誤! – bernie 2012-03-17 18:49:38

+0

當我有其他代碼的答案,並做類似的代碼錯誤,讓我生氣。謝啦!! – TLSK 2012-03-17 18:51:10