python 写入mysql

开发环境及工具:mac  jupyter-notebook


import tushare as ts
import matplotlib.pyplot as plt
import pandas as pd
import pymysql.cursors
import time
import pymysql
import numpy as np



def getstockdata(stockid, starttime, endtime):
    df = ts.get_hist_data(stockid, start=starttime, end=endtime)
    df = df.astype(str)
    # add the stockinfo to the stockdata
    df['stockid'] = stockid
    df['tradedate'] = df.index

    # create a dataframe to save the stockdata;must have the same index with df
    df_stockinfo = df[['stockid', 'tradedate']]

    # delete the stockinfo from stockhist
    df.drop(labels=['stockid', 'tradedate'], axis=1, inplace=True)
    # merge the stockinfo with stockdata and save in the df_stock
    df_stock = df_stockinfo.merge(df, right_index=True, left_index=True)
    return df_stock


# define function to convert the dataframe to list contains tuple
def convertdata(dfstock):
    list_row = dfstock.shape[0]
    # create  the list to transport values to mysql
    values = []
    for i in range(list_row):
        # chose every row and convert its type (series)   to list
        value = pd.np.array(dfstock.iloc[i])
        # convert list to tuple and add to the end of values
        tuple_value = tuple(value)
        values.append(tuple_value)
    return values

#define function to save data to mysql
def savetomysql(values):
    # Connect to the database

    connection = pymysql.connect(host='localhost',user='*******',password='*******',db='*******',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)

    try:
        with connection.cursor() as cursor:
    # Create a new record
            #sql = "INSERT INTO stocks_data('stockid','tradedate','open','high','close','low','volume','price_change','p_change','ma5','ma10','ma20','v_ma5','v_ma10','v_ma20','turnover') VALUES( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
            sql = "INSERT INTO stocks_data(stockid,tradedate,open,high,close,low,volume,price_change,p_change,ma5,ma10,ma20,v_ma5,v_ma10,v_ma20,turnover) VALUES( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"

            cursor.executemany(sql, values)

        # connection is not autocommit by default. So you must commit to save
        # your changes.
            connection.commit()  # commit changes to stable storage

    # with connection.cursor() as cursor:
    # Read a single record
    #     sql = "SELECT `stockid`, `stockname` FROM `stocks_id` "
    #     cursor.execute(sql)
    #   result = cursor.fetchone()
    #    print(result)

    except Exception as e:
        # throw the exception
        print("error:")
        print(e)
        connection.rollback()  # Roll back the current transaction


    finally:
        cursor.close()
        connection.close()


def main():
    begintime = time.time()


    # define the stockid and begintime and endtime
    stockid = '002174'
    starttime = '2017-01-01'
    endtime = '2018-01-11'
    df_stock = getstockdata(stockid, starttime, endtime)
     #every row is a row in mysql
    values = convertdata(df_stock)
    #print(values)
    savetomysql(values)
    endtime = time.time()
    print(begintime - endtime)

main()

猜你喜欢

转载自blog.csdn.net/c11611/article/details/79053278
今日推荐