我尝试使用pypyodbc模块访问远程MS SQL数据库2014.但是,当我运行以下脚本时,我收到错误消息:
Traceback (most recent call last):
File "C:\Users\username\Desktop\myscirpt.py", line 159, i
n <module>
db= pypyodbc.connect('DRIVER='+ driver + ';SERVER='+ client_db_ip +';PO
RT=' + port + ';DATABASE=' + database + ';trusted_connection=yes;')
File "C:\Python27\lib\site-packages\pypyodbc.py", line 2454, in __init__
self.connect(connectString, autocommit, ansi, timeout, unicode_results, read
only)
File "C:\Python27\lib\site-packages\pypyodbc.py", line 2507, in connect
check_success(self, ret)
File "C:\Python27\lib\site-packages\pypyodbc.py", line 1009, in check_success
ctrl_err(SQL_HANDLE_DBC, ODBC_obj.dbc_h, ret, ODBC_obj.ansi)
File "C:\Python27\lib\site-packages\pypyodbc.py", line 987, in ctrl_err
raise DatabaseError(state,err_text)
pypyodbc.DatabaseError: (u'08001', u'[08001] [Microsoft][SQL Server Native Clien
t 11.0]TCP Provider: An existing connection was forcibly closed by the remote ho
st.\r\n')
下面是db连接的一个非常简单的代码。这里的要点是我可以从同一台机器手动连接到客户机数据库,这是我的python脚本所在的机器。我很好奇究竟是什么导致了这样的问题?
driver = '{SQL Server Native Client 11.0}'
client_db_ip = 'client_db_ip' # client server ip
port = '1433'
database = 'UPG_TEST_MAIN'
db= pypyodbc.connect('DRIVER='+ driver + ';SERVER='+ client_db_ip +';PORT=' + port + ';DATABASE=' + database + ';trusted_connection=yes;')
print db
db.close()