简体   繁体   English

Python 和 SQLAlchemy 使用 For 循环打印数据库内容

[英]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.我已经尝试再次安装 sqlalchemy 并且我不知道我是否遗漏了什么或我做错了什么,因为我是 python 和 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通过运行 python3 list.py 而不是 python list.py 解决了问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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