Python 3.x 连接 pymysql 数据库

Python 3.x 连接 pymysql 数据库

首先,需要安装库:

使用 pycharm IDE,如PyCharm,可以使用 project python 安装第三方模块。

[File] >> [settings] >> [Project: python] >> [Project Interpreter] >> [Install按钮]

Python 3.x 连接 pymysql 数据库

Python 3.x 连接 pymysql 数据库

Python语句连接数据库并提取数据:

import pymysql
import pandas as pd

con = pymysql.connect(host='4', port=3, user='g', passwd='G1', db='gr', charset='GBK')
cur = con.cursor()
cur.execute("sa.tables where table_schema='g';")
tableList = cur.fetchall()
for tableName in tableList:
    print(tableName[0])
    df = pd.read_sql('SELECT * FROM ' + tableName[0], con)
    df.to_csv(tableName[0] + '.csv')