如何使用 SQLAlchemy 执行 SQL 查询,稍后将其传递到 pandas 数据帧
How to perform a SQL query with SQLAlchemy to later pass it into a pandas dataframe
我已经按照 this 文章进行设置,似乎一切正常,但我现在想执行 SQL 查询并将结果传递到 pandas 数据中框架,我该如何继续?
这就是我现在拥有的;
host_server = os.environ.get('host_server', 'localhost')
db_server_port = urllib.parse.quote_plus(str(os.environ.get('db_server_port', '5432')))
database_name = os.environ.get('database_name', 'my_data_base123')
db_username = urllib.parse.quote_plus(str(os.environ.get('db_username', 'my_user_name123')))
db_password = urllib.parse.quote_plus(str(os.environ.get('db_password', 'my_password123')))
ssl_mode = urllib.parse.quote_plus(str(os.environ.get('ssl_mode','prefer')))
DATABASE_URL = 'postgresql://{}:{}@{}:{}/{}?sslmode={}'.format(db_username, db_password, host_server, db_server_port, database_name, ssl_mode)
database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()
engine = sqlalchemy.create_engine(
DATABASE_URL, pool_size=3, max_overflow=0
)
metadata.create_all(database)
app = FastAPI(title="REST API using FastAPI PostgreSQL Async EndPoints")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
@app.on_event("startup")
async def startup():
await database.connect()
@app.on_event("shutdown")
async def shutdown():
await database.disconnect()
正在尝试使用 SQL 查询访问数据库:
with engine.connect() as conn:
result = conn.execute(text("select 'hello world'))
print(result.all())
as it says in the sqlalchemy documentation ,但我收到一些错误,例如:
print(result.all())
AttributeError: 'ResultProxy' object has no attribute 'all'
即使我尝试访问我的数据库的表
with engine.connect() as conn:
result = conn.execute(text("select * FROM users"))
print(result.all())
我得到同样的错误
解决了,只好升级SQLAlchemy
sudo pip install sqlalchemy --upgrade
我已经按照 this 文章进行设置,似乎一切正常,但我现在想执行 SQL 查询并将结果传递到 pandas 数据中框架,我该如何继续?
这就是我现在拥有的;
host_server = os.environ.get('host_server', 'localhost')
db_server_port = urllib.parse.quote_plus(str(os.environ.get('db_server_port', '5432')))
database_name = os.environ.get('database_name', 'my_data_base123')
db_username = urllib.parse.quote_plus(str(os.environ.get('db_username', 'my_user_name123')))
db_password = urllib.parse.quote_plus(str(os.environ.get('db_password', 'my_password123')))
ssl_mode = urllib.parse.quote_plus(str(os.environ.get('ssl_mode','prefer')))
DATABASE_URL = 'postgresql://{}:{}@{}:{}/{}?sslmode={}'.format(db_username, db_password, host_server, db_server_port, database_name, ssl_mode)
database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()
engine = sqlalchemy.create_engine(
DATABASE_URL, pool_size=3, max_overflow=0
)
metadata.create_all(database)
app = FastAPI(title="REST API using FastAPI PostgreSQL Async EndPoints")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
@app.on_event("startup")
async def startup():
await database.connect()
@app.on_event("shutdown")
async def shutdown():
await database.disconnect()
正在尝试使用 SQL 查询访问数据库:
with engine.connect() as conn:
result = conn.execute(text("select 'hello world'))
print(result.all())
as it says in the sqlalchemy documentation ,但我收到一些错误,例如:
print(result.all())
AttributeError: 'ResultProxy' object has no attribute 'all'
即使我尝试访问我的数据库的表
with engine.connect() as conn:
result = conn.execute(text("select * FROM users"))
print(result.all())
我得到同样的错误
解决了,只好升级SQLAlchemy
sudo pip install sqlalchemy --upgrade