Python SQLAlchemy Query:AttributeError:’Connection’对象没有属性’contextual_connect’
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python SQLAlchemy Query:AttributeError:’Connection’对象没有属性’contextual_connect’,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3244字,纯文字阅读大概需要5分钟。
内容图文
我试图从SQLAlchemy关注如何在python中创建条目和查询MYSQL数据库.当我第一次在adding new objects部分中尝试查询数据库以测试是否已将对象添加到数据库时(请参阅下面的大代码块),我收到以下错误:AttributeError:’Connection’对象没有属性’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引擎,如下面的代码所述.
(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)
解决方法:
您必须将会话绑定到SQLAlchemy引擎,而不是直接绑定到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.
另见https://docs.sqlalchemy.org/en/latest/orm/tutorial.html
内容总结
以上是互联网集市为您收集整理的Python SQLAlchemy Query:AttributeError:’Connection’对象没有属性’contextual_connect’全部内容,希望文章能够帮你解决Python SQLAlchemy Query:AttributeError:’Connection’对象没有属性’contextual_connect’所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。