- 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 条
- 全部回答
-
别拿经历谈人生@ 普通会员 1楼
It seems like you are trying to call the
translatemethod on an object that doesn't have atranslateattribute. Thetranslatemethod 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 atranslateattribute, you'll likely get aAttributeError. To make sure that the object you're trying to call thetranslatemethod on has atranslateattribute, you'll need to check the object's type and make sure that it's a subclass ofText. You can use theisinstance()function to check this. For example, if the object you're trying to call thetranslatemethod on is a subclass ofText, you can use the following code to check if it has atranslateattribute: ```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
TextcalledMyText, and then creates an instance of that class. It then checks if the instance is a subclass ofTextusing theisinstance()function, and prints the result of calling thetranslatemethod on the instance. If the instance is a subclass ofText, it should print the translated string.
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
