'builtin_function_or_method' object has no attribute 'translate'

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'

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

阅读 11.7k
2 个回答

d:Anaconda3libsite-packagespymysqlconnections.py in escape(self, obj, mapping)

810                 ret = "_binary" + ret
811             return ret

--> 812 return converters.escape_item(obj, self.charset, mapping=mapping)

813
814     def literal(self, obj):

d:Anaconda3libsite-packagespymysqlconverters.py in escape_item(val, charset, mapping)

 25         val = encoder(val, charset, mapping)
 26     else:

---> 27 val = encoder(val, mapping)

 28     return val
 29

d:Anaconda3libsite-packagespymysqlconverters.py in escape_unicode(value, mapping)

116
117 def escape_unicode(value, mapping=None):

--> 118 return u"'%s'" % _escape_unicode(value)

119
120 def escape_str(value, mapping=None):

d:Anaconda3libsite-packagespymysqlconverters.py in _escape_unicode(value, mapping)

 71     Value should be unicode
 72     """

---> 73 return value.translate(_escape_table)

 74
 75 if PY2:

AttributeError: 'builtin_function_or_method' object has no attribute 'translate'

碰到了一样的问题,抱歉我也还没有解决,搜索到了你的问题,但是没有发现答案,这个报错没办法知晓代码哪里出现了纰漏

推荐问题