Showing posts with label Unexpected EOF from the server. Show all posts
Showing posts with label Unexpected EOF from the server. Show all posts

Wednesday, July 29, 2015

pymssql 에서 한글 깨짐 문제. (pymssql.OperationalError 20017 'DB-Lib error message 20017, Unexpected EOF from the server)


import pymssql

conn = pymssql.connect(host='192.168.11.101', user='user', password='password', database='db',charset='utf8')

위 와 같이 작성해서 돌렸는데 아래와 같은 메시지를 만난다면?

pymssql.OperationalError: (20017, 'DB-Lib error message 20017, severity 9:\nUnexpected EOF from the server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n')

mssql 에 접속할때 charset이 안맞아서 그렇다.

utf8 대신에 charset='ISO-8859-1' 로 수정해서 다시 시도해본다.
그래도 안되면 charset 항목을 제외시켜도 본다.

한글을 사용하는 경우 왠만하면 빼거나, utf8 이거나 ISO-8859-1 로 해서 될것임.

문제는 데이터베이스에서 자료를 읽으면 한글이 

"Á¦±æ"  요런식으로 깨진다는것.

이 현상은 한글(euc-kr / cp949)이 ISO-8859-1로 인코딩 되어서 그런것임. 이것을 euc-kr로 변환 했다가 다시 utf-8로 변환해서 보여주면됨.

    t = row["fieldname"]
    t = t.encode('ISO-8859-1')
    t=t.decode('euc-kr')
    print(t)



그런데 connect 할때  charset을 utf8로 하지 않으면 한글이 깨지고 난리 법석이다.
utf8만 쓰는게 답이다.


예제 소스 코드
--------------------------------------------------------
#-*- coding: utf-8 -*-
__author__ = 'kdw'

import pymssql

conn = pymssql.connect(host='???????, user='????', password='????', database='???',charset='utf8')

cur = conn.cursor(as_dict=True)
cur.execute("select '제길' as damm")
row = cur.fetchone()
while row:
    t = row["damm"]
    print(t) 
    t = t.encode('ISO-8859-1')
    t=t.decode('euc-kr')
    print (t)

    row = cur.fetchone()

cur.close()
cur = None

결과: ---------------------------------------------------------------------
C:\Python27\python.exe C:/Users/kdw/PycharmProjects/untitled/test.py

Á¦±æ
제길

Process finished with exit code 0



===========================================
In English 
===========================================

change connect "charset" parameter, try again

recommand charset:
ISO-8859-1
utf8