简体   繁体   中英

SQLalchemy. I get only one result for this query

I have this python code that creates a table called 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

   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. 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".

What query method do I use?

Try with '%' after 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM