【发布时间】:2022-12-09 22:15:18
【问题描述】:
create_engine 函数和 app.config['SQLALCHEMY_DATABASE_URI'] 配置键看起来像是在做同样的事情。它们只是连接到数据库的两种不同方式吗?
我在使用 create_engine 连接的 SQLAlchemy 文档中找到了这个:
from sqlalchemy import create_engine
from sqlalchemy import text
engine = create_engine("postgresql+psycopg2://scott:tiger@localhost:5432/mydatabase")
with engine.connect() as connection:
result = connection.execute(text("select username from users"))
for row in result:
print("username:", row["username"])
但是我刚刚完成的网络应用程序只使用这个语句来连接:
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://username:password@server/db'
【问题讨论】:
标签: python flask sqlalchemy