【问题标题】:Mysqldb update error typeerror 'str' object is not callablemysqldb更新错误类型错误'str'对象不可调用
【发布时间】:2012-03-17 18:28:10
【问题描述】:

我已经用 MySQLdb 创建了一个数据库。 在数据库中,我有一个名为 student 的表,其中包含列:

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' 对象不可调用

我使用 fl 来调用,但什么都没有。
我需要一些帮助来解决这个问题。谢谢!

【问题讨论】:

  • 发布其余的错误消息。它会告诉您错误的实际位置。

标签: python mysql wxpython mysql-python wxwidgets


【解决方案1】:

可能还有其他问题,但让我们从这个开始:

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

【讨论】:

  • 这样的事情每天都会发生在我身上。去解决下一个错误!
  • 当我有其他代码的答案并且对类似代码犯下类似的错误时,我会很生气。谢谢大佬!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 1970-01-01
相关资源
最近更新 更多