inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

Python을 이용한 주가 백테스팅 시스템 구축하기

데이터베이스에 받아온 데이터 집어넣기

에러가 나는데 어디가 잘못 된걸까요?

104

captainsolo

작성한 질문수 2

0

삭제된 글입니다

pandas mysql Visual Studio Code anaconda python

답변 1

0

captainsolo

[ 전체코딩내용 ]

# pip install pywin32

from pwd import credentials

import win32com.client

import pythoncom

import time

import mysql.connector

class loginEventHandler:

    is_login=False

    def OnLogin(self,code,msg):

        print(code,msg)

        print("로그인 완료")

        loginEventHandler.is_login=True

class t8430eventHandler:

    is_called = False

    def OnReceiveData(self,tr):

        print("불러오기 완료")

        print(tr)

        t8430eventHandler.is_called = True

    

session = win32com.client.DispatchWithEvents("XA_Session.XASession",loginEventHandler)

session.ConnectServer("hts.ebestsec.co.kr",20001)

print(session.IsConnected())

if session.IsConnected():

    session.Login(credentials["id"], credentials["password"], credentials["cert_pwd"],0,0)

while loginEventHandler.is_login == False:

    pythoncom.PumpWaitingMessages()

# C:\eBEST\xingAPI\Res

t8430_sesstion = win32com.client.DispatchWithEvents("XA_DataSet.XAQuery",t8430eventHandler)

t8430_sesstion.ResFileName = 'C:\\eBEST\\xingAPI\\Res\\t8430.res'

t8430_sesstion.SetFieldData("t8430InBlock","gubun",0 ,0)

t8430_sesstion.Request(0)

while t8430eventHandler.is_called == False:

    pythoncom.PumpWaitingMessages()

count = t8430_sesstion.GetBlockCount("t8430OutBlock")

print(count)

connection = mysql.connector.connect(user="root", password="may6788", host="127.0.0.1")

cursor_a = connection.cursor(buffered=True)

cursor_a.execute("create schema backtest")

sql = "CREATE TABLE 'backtest'.'total_company_list' ('hname' VARCHAR(45) NOT NULL,'shcode' VARCHAR(20) NULL,'expcode' VARCHAR(45) NULL,'eftgubun' VARCHAR(5) NULL,'uplmtprice' INT NULL,'dnlmtprice' INT NULL,'jnilclose' INT NULL,'memedan' VARCHAR(45) NULL,'recprice' INT NULL,'gubun' VARCHAR(5) NULL)"

cursor_a.execute(sql)

cursor_a.execute("use backtest")

for index in range(count):

    hname = t8430_sesstion.GetFieldData("t8430OutBlock","hname",index)

    shcode = t8430_sesstion.GetFieldData("t8430OutBlock","shcode",index)

    expcode = t8430_sesstion.GetFieldData("t8430OutBlock","expcode",index)

    eftgubun = t8430_sesstion.GetFieldData("t8430OutBlock","etfgubun",index)

    uplmtprice = t8430_sesstion.GetFieldData("t8430OutBlock","uplmtprice",index)

    dnlmtprice = t8430_sesstion.GetFieldData("t8430OutBlock","dnlmtprice",index)

    jnilclose = t8430_sesstion.GetFieldData("t8430OutBlock","jnilclose",index)

    memedan = t8430_sesstion.GetFieldData("t8430OutBlock","memedan",index)

    recprice = t8430_sesstion.GetFieldData("t8430OutBlock","recprice",index)

    gubun = t8430_sesstion.GetFieldData("t8430OutBlock","gubun",index)

    cursor_a.execute("insert into total_company_list values ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')".format(hname,shcode,expcode,eftgubun,uplmtprice,dnlmtprice,jnilclose,memedan,recprice,gubun))

    print(hname,":", gubun)

    connection.commit()

    

[오류내용]

Traceback (most recent call last):

  File "xing.py", line 49, in <module>

    cursor_a.execute("create schema backtest")

  File "C:\Users\jeffrey\anaconda3\envs\32bit_env\lib\site-packages\mysql\connector\cursor.py", line 569, in execute      

    self._handle_result(self._connection.cmd_query(stmt))

  File "C:\Users\jeffrey\anaconda3\envs\32bit_env\lib\site-packages\mysql\connector\connection.py", line 599, in cmd_query

    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))

  File "C:\Users\jeffrey\anaconda3\envs\32bit_env\lib\site-packages\mysql\connector\connection.py", line 487, in _handle_result

    raise errors.get_exception(packet)

mysql.connector.errors.DatabaseError: 1007 (HY000): Can't create database 'backtest'; database exists

소리가 겹쳐서 들려요

0

7

2

데스크톱과 노트북 연결

0

8

0

간단한 오타 제보입니다.

0

3

0

dict, zip

0

12

2

노션 : 파트3번 링크와 권한 , 파트4번 권한요청, 파트 5번도 미리 요청 드립니다.

0

23

4

6-6 실습 문의

0

17

2

큰 범위 조회 시 EXPLAIN의 rows 값이 정확하지 않은 이유가 궁금합니다.

0

23

1

미션 06-02

0

20

2

yes24 수집 md 파일 만들 때

0

18

2

환경 세팅 관련 문의 (우분투)

0

273

1

분봉 데이터

0

260

0

질문 잇습니다

0

341

3

혹시 구글 Colab으로도 구현이 가능할까요?

0

506

2

pythoncom.PumpWaitingMessages() 질문

0

469

0

32비트 가상환경 오류

0

1068

4

왜 접속이 안될까요?

0

440

2

SELECT * FROM 구문 사용 시 UnicodeDecode 에러가 발생합니다.

0

221

0

강의 PPT가 있나요 ? 중간중간 말씀하시는데 못 찾겠습니다 ;;

0

317

2

pip install mysql-connector-python 오류

0

444

1

오류메시지가 뜨는데 한번 봐주시겠습니까?

0

208

1

안녕하세요 MySQL 서버 설치 중 Accounts and Roles 부분에서 에러가 납니다.

0

1219

2

강의에서 사용된 소스 자료를 받고 싶습니다

0

324

1

update_database 메서드에서 cursor 인식 불가

0

334

0

pwd.py 파일 관련..

0

196

1