N
N
nrv2017-03-10 15:42:58
Python
nrv, 2017-03-10 15:42:58

How to fix bugs in error messages in pyodbc/pymssql?

By themselves, Cyrillic characters are normally displayed, but in error messages (both in Python IDLE and in cmd.exe) in:
1) pymssql:

Traceback (most recent call last):
  File "mypymssql.py", line 9, in <module>
    database='SOVCOMBANK2'
  File "pymssql.pyx", line 641, in pymssql.connect (pymssql.c:10824)
pymssql.OperationalError: (18456, b'\xd0\x9e\xd1\x88\xd0\xb8\xd0\xb1\xd0\xba\xd0\xb0 \xd0\xb2\xd1\x85\xd0\xbe\xd0\xb4\xd0\xb0 \xd0\xbf\xd0\xbe\xd0\xbb\xd1\x8c\xd0\xb7\xd0\xbe\xd0\xb2\xd0\xb0\xd1\x82\xd0\xb5\xd0\xbb\xd1\x8f "nrv99".DB-Lib error message 20018, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (ASUSU38N\\SQLEXPRESS2012)\n')

As I understand it, these are character codes instead of the characters themselves.
2)pyodbc:
Traceback (most recent call last):
  File "mypyodbc.py", line 9, in <module>
    """)
pyodbc.ProgrammingError: ('42S02', '[42S02] [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]������������ ��� ������� "CLIENTS". (208) (SQLExecDirectW)')

Already normal krakozyabry.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
korifey13, 2019-09-13
@korifey13

The question was asked more than two years ago, and yet today I also received a short description of the error from the Windows MSSQL server. The problem was solved by encoding and decoding the string:

try:
    conn = pyodbc.connect(...)
except Exception as err:
    print(err.args[1].encode("cp1252").decode("cp1251"))

Where did I get cp1252 and cp1251 from? I copied the krakozyabry from the console and decoded it with the Decoder .

D
d-stream, 2017-03-10
@d-stream

Worth trying to switch encoding (chcp unicode or chcp 65001)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question