您的位置:首页 > 编程语言 > Python开发

Python 3.x 连接 pymysql 数据库

2018-01-05 16:24 531 查看
首先,需要安装库:

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

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





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')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: