python - mysqldb更新报错typeerror 'str' object is not callable

标签 python mysql wxpython mysql-python wxwidgets

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

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

我用函数 Update 更新一行。 我的代码如下:

    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 来调用但什么也没有。
我需要一些帮助来解决这个问题。谢谢!

最佳答案

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

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

关于python - mysqldb更新报错typeerror 'str' object is not callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9752623/

相关文章:

python - 如何使用 pywin32 模拟按住

python - 如何打包 python 程序以便在网络上分发

mysql - 如何将当前时间与其他已有的4个时间字段进行比较

mysql - 我可以在 openshift 中部署 mysql 并从外部访问吗

python - 是否可以在 wxPython gizmos ledctrl 上贴上标签?

python - 如何在 wxPython 中修改 TextCtrl 的宽度?

python - 我没有得到关于 os 模块的东西吗?

python - mlxtendplot_decision_regions 模型适合 Pandas DataFrame?

mysql - 拒绝用户 MySql 的访问

python - wxPython 通过 py2app : "no appropriate 64-bit architecture" ERROR even though 32-bit preference set