2012-03-17 7 views
1

MySQLdbでデータベースを作成しました。Mysqldbのエラーが発生しました。タイプエラー '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

ポストエラーメッセージの残り
しかしsql2があるだけで、文字列が含まれています。エラーが実際にどこにあるかを示します。 – kindall

答えて

4

あり、他の問題であってもよいが、のはこれで起動してみましょうことがあります。

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

...間違っています。

あなたは、引数の間にコンマが必要:this_is_how_we_call_a_func() < - 括弧の点に注意してください。

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

あなたのコードは、関数呼び出しのように見えます!証拠が表示されますよう - - 呼び出すことはできません...

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

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

Yeeeeeeees、何度か誰かが欠けているものを見つけるためにコードを見なければならない! – TLSK

+0

このようなことは毎日私に起こります。次のバグを手に入れよう! – bernie

+0

他のコードへの回答があり、同様のコードに間違いがあると、私は怒ってしまいます。ありがとう!! – TLSK

関連する問題