pandas sqlalchemy MySQL读取出错

用pandas从MySQL中读取数据时,显示SQL语句有错误:ObjectNotExecutableError: Not an executable object: 'select * from salary'

img

img

这个错误通常发生在尝试执行非执行型语句(例如SELECT语句)的情况下,可能是因为没有正确连接到MySQL数据库或者没有正确的权限执行查询操作。

下面是一个示例代码,用于连接到MySQL数据库并从salary表中读取数据:
import pandas as pd
from sqlalchemy import create_engine

#连接到MySQL数据库
engine = create_engine('mysql+pymysql://username:password@host:port/database')

#读取数据
df = pd.read_sql_query('SELECT * FROM salary', engine)

#显示前5行数据
print(df.head())

请注意,上述代码中的username,password,host,port和database应该根据您自己的数据库配置进行更改。如果您仍然遇到相同的错误,请检查您是否具有执行查询操作的权限,并确保您的SQL语句正确无误。