使用Sql,flask,sqlalchemy将数据库连接到Flask应用

时间:2019-06-25 04:08:22

标签: python sql postgresql flask sqlalchemy

我正在尝试将python cmd line应用程序连接到我的数据库,“遵循harvardx cs50教程”。

我尝试更改数据库路径,安装了d = {'Date': ['03-Jan-11', '04-Jan-11', '05-Jan-11', '06-Jan-11', '12-Mar-11', '05-Apr-11', '08-Jul-11', '05-Jan-12'], 'Price': [112, 115, 116, 111, 120, 118, 125, 114]} df = pd.DataFrame(data=d) df['Date'] = pd.to_datetime(df['Date'], errors='coerce') df['quarter'] = pd.PeriodIndex(df.Date, freq='Q') df.groupby(['quarter']).mean() out put quarter Price 2011Q1 114.8 2011Q2 118.0 2011Q3 125.0 2012Q1 114.0

sqlalchemy

我收到以下错误消息:

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__ == "__name__":
    main()

0 个答案:

没有答案