import pandas as pd
import sqlalchemy
from sqlalchemy import create_engine
# 用sqlalchemy构建数据库链接engine
connect_info = 'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8'
engine = create_engine(connect_info)
# sql 命令
sql_cmd = "SELECT * FROM table"
df = pd.read_sql(sql=sql_cmd, con=engine)
pandas.read_sql(sql,con,index_col = None,coerce_float = True,params = None,parse_dates = None,columns = None,chunksize = None)
参数 | 描述 |
---|---|
sql | string或SQLAlchemy,要执行的查询命令 |
con | 连接引擎 |
index_col | string或list,可选,默认无。将指定列作为pandas的索引列 |
coerce_float | boolean,默认True。尝试将非字符串,非数字对象转换为浮点 |
params | list,tuple或dict,默认None。向sql语句中传递参数 |
parse_dates | list或dict,默认None。将指定列解析为日期格式,datetime型数据,与pd.to_datetime函数功能类似。可以用字典的格式提供列名和转换的日期格式,比如{column_name: format string}(format string:"%Y:%m:%H:%M:%S") |
columns | list,默认为None。指定读取的列名,一般不用,在sql中指定 |
chunksize | int,默认为None。每次读取的行数 |
返回 | DataFrame |
,
参考:
https://www.cjavapy.com/article/143/
https://www.cnblogs.com/cymwill/p/8289367.html