使用pandas连接mysql数据库并获取数据
需要先安装3个第三方包
首先,连接数据库并执行sql
import pymysql import pandas as pd from sqlalchemy import create_engine #Python中一个通过 ORM 操作数据库的框架 def reader(sql): engine = create_engine(‘mysql+pymysql://root::3306/crm_info?charset=utf8‘) data = pd.read_sql(sql,engine) return data pddata = reader("select * from zj_zjxx") pddata.head()
使用pandas中DataFrame.to_sql()可以将数据导入到数据库表中
import pymysql import pandas as pd ym = pd.read_csv("C:\\Users\\52745\\Jupyter_doc\\ym\\广东省.csv") ym.to_sql(name = ‘ym2‘,con = ‘mysql+pymysql://root::3306/crm_info?charset=utf8‘,if_exists="append",index=False)
相关推荐
三石 2020-10-30
三石 2020-10-29
roamer 2020-10-29
wangquannuaa 2020-10-15
wangquannuaa 2020-09-29
jzlixiao 2020-09-15
wangquannuaa 2020-08-30
三石 2020-08-23
逍遥友 2020-08-21
jzlixiao 2020-08-18
wangquannuaa 2020-08-17
QianYanDai 2020-08-16
cjsyrwt 2020-08-14
jzlixiao 2020-07-29
xirongxudlut 2020-07-20
mmmjyjy 2020-07-16
QianYanDai 2020-07-05
QianYanDai 2020-07-05
june0 2020-07-04