dailykt mysql replace 数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/luoganttcc/article/details/89405689
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 18 18:55:47 2019

@author: lg
"""

import pandas as pd
import pymysql
from sqlalchemy import create_engine
engine = create_engine("mysql+pymysql://root:[email protected]:3306/stock?charset=utf8") 
import tushare as ts 

import pymysql.cursors
#连接配置信息
config = {
'host':'127.0.0.1',
'port':3306,
'user':'root',
'password':'root',
'db':'stock',
'charset':'utf8',
'cursorclass':pymysql.cursors.DictCursor,
}
# 创建连接
connection = pymysql.connect(**config)
cursor = connection.cursor()

cons = ts.get_apis()
ts.set_token('46fcca1e059c38cde5f56fe7748f53c274036cb8cf0c061c2056alg690')
pro = ts.pro_api()
pp=pro.daily_basic(ts_code='', trade_date='20190418')
code=list(pp['ts_code'])
connection = pymysql.connect(**config)
cursor = connection.cursor()

def savek(k):
    

    df = pro.daily(ts_code=k,adj='qfq', start_date='20190418', end_date='20190419')
    
#    df.to_sql(name = 'dailykt',con = engine,if_exists = 'append',index = False,index_label = False)
    
    num=df.shape[0]
#    
    for p in range(num):
        ty=df.iloc[p]
        
#    s1=(`ts_code`, `trade_date`, `open`, `high`, `low`, `close`, `pre_close`,`change`, `pct_chg`, `vol`, `amount`)
    
    
        pp=tuple(ty.values)
        sql = "REPLACE INTO `dailykt` (`ts_code`, `trade_date`, `open`, `high`, `low`, `close`, `pre_close`,`change`, `pct_chg`, `vol`, `amount`) VALUES " +str(pp)
        cursor.execute(sql)
    connection.commit()  
     
    return None
     
     
for h in code:
    print(h)
    savek(h)
    

猜你喜欢

转载自blog.csdn.net/luoganttcc/article/details/89405689
今日推荐