python 用 pymsql 连接数据库

1、加载库

import platform, sys
import pandas as pd
import pymysql

2、显示环境配置

 

 3、连接数据库

 

 4、查询数据库 show databases

 

 函数式查找

def show_databases():
    sql = 'show databases;'
    try:
        connection = connection  # 连接
    except:
        connection = pymysql.connect(**config)
    cursor = connection.cursor()  # 创建光标
    cursor.execute(sql)  # 执行查询
    dbs = cursor.fetchall()
    header = cursor.description[0][0]
    df = pd.DataFrame([db for db, in dbs], columns=[header])  # 创建数据框
    return df

调用函数

posted @ 2019-08-01 22:56  赏尔  阅读(281)  评论(0编辑  收藏  举报