账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    'builtin_function_or_method' object has no attribute 'translate'
    38
    0

    1.在执行await cur.execute(sql, args)时,报错AttributeError: 'builtin_function_or_method' object has no attribute 'translate'

    这里的代码是根据廖雪峰的python教程写的实战代码,在进行用户注册功能测试执行保存时报错

    2.代码

    async def execute(sql,args,autocommit=True):
        log(sql)
        async with __pool.get() as conn:
            if not autocommit: # 如果不是自动提交
                await conn.begin()
            try:
                async with conn.cursor(aiomysql.DictCursor) as cur:
                    sql = sql.replace('?', '%s')
                    await cur.execute(sql, args)
                    affected = cur.rowcount # 获取sql语句影响的行数
                if not autocommit:
                    await conn.commit()
            except BaseException as e:
                print('e: ',e)
                if not autocommit:
                    await conn.rollback()
                raise
            return affected

    3.报错信息

    INFO:root:call with args: {'email': 'dd@qq.com', 'name': 'dd', 'passwd': 'f23ab4e297b7b5632c7bd9c816281802ea5c51cf'}
    INFO:root:SQL: select `id`, `email`, `passwd`, `admin`, `name`, `image`, `create_at` from `users` where email=?
    INFO:root:rows returned: 0
    INFO:root:SQL: insert into `users`(`email`, `passwd`, `admin`, `name`, `image`, `create_at`, `id`) values(?,?,?,?,?,?,?)
    e:  'builtin_function_or_method' object has no attribute 'translate'
    ERROR:aiohttp.server:Error handling request
    Traceback (most recent call last):
      File "D:\Python\awesome-python3-webapp\venv\lib\site-packages\aiohttp\web_protocol.py", line 385, in start
        resp = await self._request_handler(request)
      File "D:\Python\awesome-python3-webapp\venv\lib\site-packages\aiohttp\web_app.py", line 338, in _handle
        resp = await handler(request)
      File "D:\Python\awesome-python3-webapp\venv\lib\site-packages\aiohttp\web_middlewares.py", line 88, in impl
        return await handler(request)
      File "D:/Python/awesome-python3-webapp/www/app.py", line 38, in logger
        return (await handler(request))
      File "D:/Python/awesome-python3-webapp/www/app.py", line 58, in response
        r = await handler(request)
      File "D:\Python\awesome-python3-webapp\venv\lib\site-packages\aiohttp\web_urldispatcher.py", line 111, in handler_wrapper
        result = await result
      File "D:\Python\awesome-python3-webapp\www\coroweb.py", line 135, in __call__
        r = await self._func(**kw)
      File "C:\Python36\lib\asyncio\coroutines.py", line 225, in coro
        res = yield from await_meth()
      File "D:\Python\awesome-python3-webapp\www\handlers.py", line 150, in api_register_user
        await user.save()
      File "D:\Python\awesome-python3-webapp\www\orm.py", line 235, in save
        rows = await execute(self.__insert__, args)
      File "D:\Python\awesome-python3-webapp\www\orm.py", line 52, in execute
        await cur.execute(sql, args)
      File "D:\Python\awesome-python3-webapp\venv\lib\site-packages\aiomysql\cursors.py", line 238, in execute
        query = query % self._escape_args(args, conn)
      File "D:\Python\awesome-python3-webapp\venv\lib\site-packages\aiomysql\cursors.py", line 197, in _escape_args
        return tuple(conn.escape(arg) for arg in args)
      File "D:\Python\awesome-python3-webapp\venv\lib\site-packages\aiomysql\cursors.py", line 197, in <genexpr>
        return tuple(conn.escape(arg) for arg in args)
      File "D:\Python\awesome-python3-webapp\venv\lib\site-packages\aiomysql\connection.py", line 366, in escape
        return escape_item(obj, self._charset)
      File "D:\Python\awesome-python3-webapp\venv\lib\site-packages\pymysql\converters.py", line 27, in escape_item
        val = encoder(val, mapping)
      File "D:\Python\awesome-python3-webapp\venv\lib\site-packages\pymysql\converters.py", line 118, in escape_unicode
        return u"'%s'" % _escape_unicode(value)
      File "D:\Python\awesome-python3-webapp\venv\lib\site-packages\pymysql\converters.py", line 73, in _escape_unicode
        return value.translate(_escape_table)
    AttributeError: 'builtin_function_or_method' object has no attribute 'translate'

    来来来,各位大神看一看:)

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 别拿经历谈人生@ 普通会员 1楼

      It seems like you are trying to call the translate method on an object that doesn't have a translate attribute. The translate method is typically used to translate text or other text-based elements in a program, such as a string or a webpage. If you're trying to call this method on an object that doesn't have a translate attribute, you'll likely get a AttributeError. To make sure that the object you're trying to call the translate method on has a translate attribute, you'll need to check the object's type and make sure that it's a subclass of Text. You can use the isinstance() function to check this. For example, if the object you're trying to call the translate method on is a subclass of Text, you can use the following code to check if it has a translate attribute: ```python class MyText(Text): def translate(self, string): return self.text.translate(string)

      my_text = MyText() if isinstance(my_text, MyText): print(my_text.translate('hello')) ```

      This code defines a subclass of Text called MyText, and then creates an instance of that class. It then checks if the instance is a subclass of Text using the isinstance() function, and prints the result of calling the translate method on the instance. If the instance is a subclass of Text, it should print the translated string.

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部