热线电话:13121318867

登录
2019-02-22 阅读量: 714
tushare连接mysql问题
systemOSX 10.11.6
python version:Python 3.5.2 :: Anaconda custom (x86_64)
tushare version:0.5.0
mysql version:Ver 14.14 Distrib 5.7.14, for osx10.11 (x86_64) using EditLine wrapper

官方示例:

from sqlalchemy import create_engine
import tushare as ts

df = ts.get_tick_data('600848', date='2014-12-22')
engine = create_engine('mysql://user:passwd@127.0.0.1/db_name?charset=utf8')

#存入数据库
df.to_sql('tick_data',engine)
#追加数据到现有表
#df.to_sql('tick_data',engine,if_exists='append')

报错信息:

ImportError: No module named 'MySQLdb'

解决方案:

python3.5不支持使用mysqldb ,使用pymysql或者mysql.connector替代。

import tushare as ts
import sys
from sqlalchemy import create_engine

def industrytodb():
#获取sina行业分类信息
industry_sina = ts.get_industry_classified("sina")
print(industry_sina, sep=' ', end='\n', file=sys.stdout, flush=False)
#获取申万行业分类信息
industry_sw = ts.get_industry_classified("sw")
print(industry_sw, sep=' ', end='\n', file=sys.stdout, flush=False)
# engine = create_engine('mysql://root:123456@localhost/stockdb?charset=utf8')
print("连接数据库", sep=' ', end='\n', file=sys.stdout, flush=False)
engine = create_engine('mysql+pymysql://root:123456@localhost/stockdb?charset=utf8')
print(engine, sep=' ', end='\n', file=sys.stdout, flush=False)
# industry_sina.to_sql('industry_sina_data',engine,if_exists='append')
# industry_sw.to_sql('industry_sw_data',engine,if_exists='append')
industry_sina.to_sql('industry_sina_data',engine)
industry_sw.to_sql('industry_sw_data',engine)

if __name__ == '__main__':
# 获取sina和申万行业分类信息
industrytodb()
0.0000
1
关注作者
收藏
评论(0)

发表评论

暂无数据