sql读取数据直接存成pandas

导包

import pymysql
import pandas as pd

获取mysql链接

def get_db():
    #打开数据库连接
    db = pymysql.connect(
        host='*.*.*.*',
        port=3306,
        user = "wws",
        passwd = "yourpasswd",
        db = "youdb")
    return db
db = get_db()

写sql 读数据保存

sql = "select `name`,`name_en`,`collage_name`,`description` from `yourdb`;"
#cursor= db.cursor()
sql_data = pd.read_sql_query(sql,db)
df = pd.DataFrame(sql_data, columns=[ 'name','name_en','collage_name','description'])
df

sql读取数据直接存成pandas_第1张图片
成了!
or

sql = "select `name`,`name_en`,`collage_name`,`description` from `yourdb`;"
cursor= db.cursor()
cursor.execute(sql)
sql_data = cursor.fetchall()
df = pd.DataFrame(sql_data, columns=[ 'name','name_en','collage_name','description'])
df

你可能感兴趣的:(Pytorch,sql,pandas,python)