from sqlalchemy import create_engine
import pymysql
import urllib.parse
import pandas as pd
from pykrx import stock
import time

pymysql.install_as_MySQLdb()
engine = create_engine("mysql+mysqldb://root:"+"swhacademy!"+"@dev-swh.ga/market", encoding='utf-8')
conn = engine.connect()

# 전체
# tickers = stock.get_market_ticker_list()
# for ticker in tickers:
#     df = stock.get_market_ohlcv_by_date("20190101", "20190131", ticker)
#     if len(df) > 0 :
#         df['종목코드'] = ticker
#         df.to_sql(name="stock_trade", con=conn, if_exists='append')
#         time.sleep(0.2)

# SK하이닉스만
ticker = '000660'
df = stock.get_market_ohlcv_by_date("20150101", "20191231", ticker)
if len(df) > 0 :
    df['종목코드'] = ticker
    print(type(df['종목코드']))
    # df.to_sql(name="stock_trade", con=conn, if_exists='append')

