如何在 Python 中创建 'Engine' 对象后读取/查询 SQL table?

How to read / query SQL table after creating an 'Engine' object in Python?

我正在使用 Python 3.7,我对 SQL 和 Python 不是很熟悉,我有下面的代码来创建一个 SQL 引擎:

mysql_engine = sqlalchemy.create_engine('mysql://xxxxxx')

然后我有一个预定义的 SQL 查询,这是一个简化版本:(这个查询我在 MySQL Workbench 中测试过并且工作正常)

example_query = '''
select
    a.date
from
    abc.xyz a
'''

我希望能够从tableread/query,我试过:

cursor = mysql_engine.cursor()
cursor.execute(example_query)

但我收到错误 AttributeError: 'Engine' object has no attribute 'cursor',也许我可以使用 pandas?有人可以帮忙吗,谢谢。

pd.read_sql

第二个参数可以接受 sqlalchemy 引擎。

来自Docs

con: SQLAlchemy connectable, str, or sqlite3 connection Using SQLAlchemy makes it possible to use any DB supported by that library. If a DBAPI2 object, only sqlite3 is supported. The user is responsible for engine disposal and connection closure for the SQLAlchemy connectable; str connections are closed automatically.

pd.read_sql(example_query, mysql_engine)