我正在使用SQLalchemy并已将数据输入数据库:
class Directions(db.Model):
id = db.Column(db.Integer, primary_key=True)
key = db.Column(db.String(16), index=True, unique=False)
现在,我正在尝试搜索给定的密钥:
Directions.query.filter(Directions.key=={some string})
但我明白了:
<flask_sqlalchemy.BaseQuery object at 0x103df57b8>
如何揭示实际的字符串?我花了两个小时搜索文档,但是1319页的haystack没用。
答案 0 :(得分:3)
filter
方法返回一个BaseQuery
对象,您可以在其上链接多个过滤器。您可以使用first
或all
来获取当前查询的结果。
答案 1 :(得分:2)
尝试使用:
directions = Directions.query.filter_by(key={some string}).first()
print(directions)
答案 2 :(得分:0)
您必须打开会话并使用会话对象的查询方法。例如:
engine = create_engine(<db url>)
Session = sessionmaker(bind=engine)
with Session() as sess:
sess.query(Directions).filter(Direction.key=={some string})
Session Documentation中描述了导致Session
调用的代码,并且可能会针对您的应用程序进行更改。您也可以在docs中阅读有关查询对象的更多信息。