Python+pandas连接MySQL查询

import pymysql
import pandas as pd
from sqlalchemy import create_engine

pd.set_option('display.max_columns', 1000)
pd.set_option('display.width', 1000)
pd.set_option('display.max_colwidth', 1000)
pd.set_option('display.max_rows', 1000)
# 这两个参数的默认设置都是False
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)

    def sina_account(self, sql):
        # 初始化数据库连接,使用pymysql模块
        # MySQL的用户:root, 密码:147369, 端口:3306,数据库:mydb
        self.engine = create_engine('mysql+pymysql://root:[email protected]:3306/mysql')

        # read_sql_query的两个参数: sql语句, 数据库连接
        df = pd.read_sql_query(sql, self.engine)

        return df

  

猜你喜欢

转载自www.cnblogs.com/mtfan01/p/11481215.html