1.原生连接pymsql
import pymysql # 安装这个包
conn = pymysql.Connect( # 建立连接
host = '127.0.0.1', # localhost
user = 'root',
password = '123456',
database = 'data',
port = 3306) #不要加引号
cur = conn.cursor() # 建立游标
cur.execute("SELECT * FROM data.company ")
data = cur.fetchall() # 获得sql结果并赋值
print(data[0])
cur.close()
conn.close()
2.pd连接
sql = 'select * from company'
df = pd.read_sql(sql,con)
result.to_csv('路径',index = 0)
参考:https://www.jianshu.com/p/02cc142d950e
七周数据分析