1

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 select statement will return multiple rows. I tried using some older code that worked before I started SQLAlchemy, but db is a SQLAlchemy object and doesn't have the execute() method.

application = Applications.query.filter_by(brochureID=brochure.id)
cur = db.execute(application)
entries = cur.fetchall()

and then in my HTML file

      {% for entry in entries %}
      var getEmail = {{entry.2|tojson|safe}}
      emailArray.push(getEmail);

I looked in the SQLAlchemy documentation and I couldn't find a .first() equivalent to getting all the rows. Can anyone point me in the right direction? No doubt it's something very small.

2 Answers 2

4

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()
Sign up to request clarification or add additional context in comments.

4 Comments

that works great pulling from the database, I can see Application objects in entries by using dump(), however when I pass entries to my HTML file, it's saying it's undefined. Any ideas why that might be the case?
Just to clarify (edit button seems to have disappeared). Entries in my HTML file has the correct number of objects, but each entry is undefined. I'm using the same code as I posted from my HTML file above
Sorry, I actually just got it sorted, I was accessing properties of the entry like this entry.1 when I should have been using column names i.e. entry.id or entry.brochureName Thanks for your help!
That was going to be my next question. Glad you got it working.
2

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()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.