⑴ mysql怎麼做股票資料庫
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import tushare as ts
from sqlalchemy import create_engine
code_list = []
#資料庫鏈接參數
host = '10.0.0.5'
port = 3306
user = 'root'
password = 'qweqwe'
database = 'stock'
charset = "utf8"
table = "stock_data"
# create table stock_data(id int auto_increment,code int(6) zerofill,date date not null,open decimal(10,2) not null,high decimal(10,2) not null,close decimal(10,2) not null,low decimal(10,2) not null,volume decimal(10,2),turnover decimal(10,2),primary key (id),index(code),index(date),index(open),index(high),index(close),index(low));
def get_data (code):
df = ts.get_hist_data(code)
data = df.iloc[::-1, [0, 1, 2, 3, 4, 13]]
data["code"] = code
return data
def save_data_to_mysql (data,user = 'root',password = 'qweqwe',database = 'stock',charset = "utf8",table = "stock_data"):
engine = create_engine('mysql://' + user + ':' + password + '@' + host + '/' + database + '?charset=' + charset)
data.to_sql(table, engine, if_exists='append')
return
for code in code_list:
try:
data = get_data(code)
save_data_to_mysql(data)
print str(code) + "導入mysql成功"
except:
print str(code) + "獲取數據失敗"