Python腳本采集某一臺SQL Server服務器資料的時候,突然遇到“Connection to the database failed for an unknown reason”錯誤,更詳細的資訊如下
Traceback (most recent call last):
File "src/pymssql.pyx", line 636, in pymssql.connect
File "src/_mssql.pyx", line 1957, in _mssql.connect
File "src/_mssql.pyx", line 677, in _mssql.MSSQLConnection.__init__
_mssql.MSSQLDriverException: Connection to the database failed for an unknown reason.During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/konglb/python/get_server_info.py", line 84, in <module>
autocommit=True);
File "src/pymssql.pyx", line 645, in pymssql.connect
pymssql.InterfaceError: Connection to the database failed for an unknown reason.During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/konglb/python/get_server_info.py", line 474, in <module>
logger.error(fe.message)
AttributeError: 'InterfaceError' object has no attribute 'message'

后面經過仔細的對比檢查、驗證測驗,終于發現這臺機器的IP地址后面有空格(一個表存盤這些資訊服務器資訊,但是最近這個新增的記錄在INSERT的時候,SERVER_IP欄位多了一個空格),直接導致Python腳本使用pymssql連接資料庫的時候遇到這個錯誤,后面我實驗驗證了一把,只要pymssql.connect的host欄位的變數如果有空格,就會報這個錯誤,特此記錄一下這個比較特殊的錯誤!
...................................................
for row in rows:
try:
src_db_conn = pymssql.connect(
host=row['SERVER_IP'], user=row['USER_NAME'], password=row['PASSWORD'], database='master', charset="utf8",autocommit=True
);
sub_cursor =src_db_conn.cursor(as_dict=True)
................................................
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/36414.html
標籤:Python
上一篇:Geany運行python檔案無法顯示中文的解決辦法
下一篇:R語言入門:向量索引
