我试图使用python中的一个对象示例创建一个数据库,但收到了如下错误消息:“attributeerror:'eu'object has no attribute'translate'”我不知道如何修复该错误
抱歉伙计们我的英语不是很好这是我的代码:
import pymysql.cursors
class Eu:
def __init__(self, nome='mel', idade=22):
self.nome = nome
self.idade = idade
eu = Eu()
nome = eu.nome
idade = eu.idade
# connect do database
conn = pymysql.connect(host='localhost',
user='root',
password='')
# create a cursor and a database
conn.cursor().execute('CREATE DATABASE banco73')
conn.cursor().execute('USE banco73')
# create tables
conn.cursor().execute('CREATE TABLE tabela73 (eu BLOB)')
# use tables
conn.cursor().execute("""INSERT INTO tabela73 VALUES (?)""", (eu))
整个控制台消息:
runfile('C:/Users/Usuario/.spyder-py3/temp.py', wdir='C:/Users/Usuario/.spyder-py3')
Traceback (most recent call last):
File "<ipython-input-59-9994dddea11c>", line 1, in <module>
runfile('C:/Users/Usuario/.spyder-py3/temp.py', wdir='C:/Users/Usuario/.spyder-py3')
File "C:\Users\Usuario\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
execfile(filename, namespace)
File "C:\Users\Usuario\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Usuario/.spyder-py3/temp.py", line 36, in <module>
conn.cursor().execute("""INSERT INTO tabela73 VALUES (?)""", (eu))
File "C:\Users\Usuario\Anaconda3\lib\site-packages\pymysql\cursors.py", line 168, in execute
query = self.mogrify(query, args)
File "C:\Users\Usuario\Anaconda3\lib\site-packages\pymysql\cursors.py", line 147, in mogrify
query = query % self._escape_args(args, conn)
File "C:\Users\Usuario\Anaconda3\lib\site-packages\pymysql\cursors.py", line 133, in _escape_args
return conn.escape(args)
File "C:\Users\Usuario\Anaconda3\lib\site-packages\pymysql\connections.py", line 839, in escape
return converters.escape_item(obj, self.charset, mapping=mapping)
File "C:\Users\Usuario\Anaconda3\lib\site-packages\pymysql\converters.py", line 27, in escape_item
val = encoder(val, mapping)
File "C:\Users\Usuario\Anaconda3\lib\site-packages\pymysql\converters.py", line 118, in escape_unicode
return u"'%s'" % _escape_unicode(value)
File "C:\Users\Usuario\Anaconda3\lib\site-packages\pymysql\converters.py", line 73, in _escape_unicode
return value.translate(_escape_table)
AttributeError: 'Eu' object has no attribute 'translate'
1条答案
按热度按时间sz81bmfz1#
不能将python对象直接存储在关系数据库中。您要做的是存储在需要时可用于重新生成对象的数据。
有一些模块对此有所帮助。它们被称为orm对象关系模型
我推荐炼金术: