import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine("postgresql://postgres:114920@localhost/Databases")
db = scoped_session(sessionmaker(bind=engine))
def main():
Railway = db.execute("SELECT origin, destination, duration FROM Railway").fetchall()
for railway in Railway:
print(f"{railway.origin} to {railway.destination}, {railway.duration} minutes")
if __name__ == "__main__":
main()
并得到了这个错误(如果可能的话,请分享代码,我被困在这个敌人很长一段时间)
C:\Web开发\Lecture 02
看起来您没有psycopg2模块。请尝试pip安装psycopg2并在终端中运行此命令
pip install psycopg2
import os
从sqlalchemy导入从sqlalchemy创建引擎。orm导入作用域_会话,sessionmaker
引擎=创建引擎(“postgresql://postgres:114920@localhost:5432/postgres“)db=scoped_会话(sessionmaker(bind=engine))
def main():铁路=db.execute("SELECT出发地,目的地,持续时间从铁路"). finchall()铁路中的铁路:print(f"{railway.origin}到{railway.destination},{railway.duration}分钟")
如果名称==“main”:main()