打开数据库连接
import pandas as pd
import pymysql
db=pymysql.connect(host="39.108.89.22",user="testdb_read",db="test4_sql",password='password',port=13306,charset="utf8")
host:端口,user:用户名,db:数据库名称,password:密码。,charset:字符形式。
使用cursor()方法获取操作游标
cursor=db.cursor()
执行sql语句
sql='''
select a.id_ '订单号',
h.item_name '进件步骤'
from msys_apply as a
left join statistic_hopper as h on a.id_=h.apply_id
'''
cursor.execute(sql)
获取全部数据
cursor.fetchall()
将数据先转换成list,再转化成DataFrame。
result =pd.DataFrame(list(cursor.fetchall()),columns=['订单号', '进件步骤'])