我正在尝试关注这个 tutorial来自 SQLAlchemy
关于如何在 python 中创建条目和查询 MYSQL 数据库。当我在他们的 adding new objects 中第一次尝试查询数据库时测试对象是否已添加到数据库的部分(请参阅下面的大代码块),我收到以下错误:AttributeError: 'Connection' object has no attribute 'contextual_connect'
我可以查询数据库。例如,如果我将最后一行代码更改为 our_user = session.query(User).filter_by(name='ed')
它会成功返回一个查询对象,但我不知道如何从这个查询结果中得到我输入数据库的对象。
同样,如果我尝试按照他们在 querying 中建议的那样遍历结果 部分:
for instance in session.query(User).order_by(User.id):
print instance.name, instance.fullname
我得到了同样的错误。我该如何修复这个特定错误,是否有任何其他关于在 Python 中使用 MYSQL 和 SQLAlchemy 的教程可以指点我?
我的代码:
import MySQLdb
from sqlalchemy import create_engine
db1 = MySQLdb.connect(host="127.0.0.1",
user="root",
passwd="****",
db="mydata")
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
from sqlalchemy import Column, Integer, String
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
fullname = Column(String)
password = Column(String)
def __init__(self, name, fullname, password):
self.name = name
self.fullname = fullname
self.password = password
def __repr__(self):
return "<User('%s','%s', '%s')>" % (self.name, self.fullname, self.password)
ed_user = User('ed', 'Ed Jones', 'edspassword')
from sqlalchemy.orm import sessionmaker
Session = sessionmaker()
Session.configure(bind=db1)
session = Session()
session.add(ed_user)
our_user = session.query(User).filter_by(name='ed').first()
更新/工作代码:
(1) 更改为 SQLAlchemy 引擎,如下面的 Codeape 所讨论的。
(2)记得创建表:Base.metadata.create_all(engine)
(3) 使用 SQLAlchemy 教程中的 User
类的“万无一失”版本。请注意 SQLAlchemy,我们(至少我)觉得自己像个傻瓜,希望您在教程的主体中始终使用万无一失的版本,而不是让忙碌的读者可能会跳过的旁白。
生成工作代码的所有内容:
import MySQLdb
from sqlalchemy import create_engine
engine = create_engine("mysql://user:password@host/database")
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
from sqlalchemy import Column, Integer, String, Sequence
class User(Base):
__tablename__ = 'users'
id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
name = Column(String(50))
fullname = Column(String(50))
password = Column(String(12))
def __init__(self, name, fullname, password):
self.name = name
self.fullname = fullname
self.password = password
def __repr__(self):
return "<User('%s','%s', '%s')>" % (self.name, self.fullname, self.password)
Base.metadata.create_all(engine)
ed_user = User('ed', 'Ed Jones', 'edspassword')
from sqlalchemy.orm import sessionmaker
Session = sessionmaker()
Session.configure(bind=engine)
session = Session()
session.add(ed_user)
our_user = session.query(User).filter_by(name='ed').first()
print(our_user is ed_user)
最佳答案
您必须将 session 绑定(bind)到 SQLAlchemy 引擎,而不是直接绑定(bind)到 MySQLDb 连接对象。
engine = create_engine("mysql://user:password@host/dbname")
Session.configure(bind=engine)
(您可以删除您的 db1
变量。)
来自教程:
The return value of create_engine() is an instance of Engine, and it represents the core interface to the database, adapted through a dialect that handles the details of the database and DBAPI in use.
关于Python SQLAlchemy 查询 : AttributeError: 'Connection' object has no attribute 'contextual_connect' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19284012/