SQLAlchemy get every row the matches query and loop through them

后端 未结 2 477
说谎
说谎 2021-01-23 18:47

I\'m new to Python and SQLAlchemy. I\'ve been playing about with retrieving things from the database, and it\'s worked every time, but im a little unsure what to do when the sel

相关标签:
2条回答
  • 2021-01-23 19:17

    the Usual way to work with orm queries is through the Session class, somewhere you should have a

    engine = sqlalchemy.create_engine("sqlite:///...")
    Session = sqlalchemy.orm.sessionmaker(bind=engine)
    

    I'm not familiar with flask, but it likely does some of this work for you.

    With a Session factory, your application is instead

    session = Session()
    entries = session.query(Application) \
              .filter_by(...) \
              .all()
    
    0 讨论(0)
  • 2021-01-23 19:30

    Your query is correct, you just need to change the way you interact with the result. The method you are looking for is all().

    application = Applications.query.filter_by(brochureID=brochure.id)
    entries = application.all()
    
    0 讨论(0)
提交回复
热议问题