[英]SQLalchemy. I get only one result for this query
I have this python code that creates a table called Events 我有此python代码可创建一个名为Events的表
class Events(Base):
__tablename__ = 'events'
id = Column(Integer, primary_key=True)
name = Column(String(25), nullable=False)
email = Column(String(30), nullable=True)
phone_number = Column(String(15), nullable=True)
venue = Column(String(70), nullable=True)
description = Column(String(2000), nullable=True)
date = Column(String(15), nullable=True)
time = Column(String(10),nullable=True)
duration = Column(String(25),nullable=True)
who_made_me = Column(String(25),nullable=True)
address = Column(String(50),nullable=True)
and after adding events y, and ye 在添加事件y和ye之后
add = Events(name = "y")
add_two = Events(name = "ye")
con.add(add,add_two)
con.commit()
I want to search for y and get both results. 我想搜索y并获得两个结果。 I tried to use:
我尝试使用:
search_results= con.query(Events).filter(Events.name.like("y")).all()
and I only get one result, "y", I would want any result that's similar to "y" like "ye" or "yr". 而我只得到一个结果“ y”,我想要任何类似于“ y”的结果,例如“ ye”或“ yr”。
What query method do I use? 我使用哪种查询方法?
Try with '%' after y. 尝试在y之后加上'%'。
search_results= con.query(Events).filter(Events.name.like("y%")).all()
Let us know whether it works. 让我们知道它是否有效。
You only get one results in your DB because you only commit one event with your session. 在数据库中只能得到一个结果,因为在会话中仅提交一个事件。 Try:
尝试:
add = Events(name = "y")
add_two = Events(name = "ye")
con.add(add)
con.add(add_two)
con.commit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.