简体   繁体   中英

Python and SQLAlchemy Printing Database Contents Using For Loop

I'm trying to run the program below

import os

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))

def main():
    flights = db.execute("SELECT origin, destination, duration FROM flights").fetchall()
    for flight in flights:
        print(f"{flight.origin} to {flight.destination}, {flight.duration} minutes.")

if __name__ == "__main__":
    main()

but it seems to have this syntax error.

在此处输入图像描述

I already tried installing sqlalchemy again and i dont know if I'm missing something or what i've done wrong since I'm new to python and sql. I've also made sure that the table already exists as seen below.

在此处输入图像描述

Thanks in advance!

fixed the problem by running python3 list.py instead of python list.py

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