Pyodbc:无法连接

时间:2017-01-24 07:58:17

标签: python pyodbc pythonanywhere

主持人:pythonanywhere.completion Python 2.7 + pyodbc

我尝试连接到SQL的远程服务器。 地址和端口是正确的。我用“......”代替了它们。

返回错误:无法连接:Adaptive Server不可用或不存在 我的代码中出现错误。请帮忙

    [odbc.log


[ODBC][17658][1485155828.109606][__handles.c][450]
  Exit:[SQL_SUCCESS]
   Environment = 0x1a21380
[ODBC][17658][1485155828.120011][SQLSetEnvAttr.c][182]
  Entry:            
   Environment = 0x1a21380            
   Attribute = SQL_ATTR_ODBC_VERSION            
   Value = 0x3            
   StrLen = 4
[ODBC][17658][1485155828.122501][SQLSetEnvAttr.c][349]
  Exit:[SQL_SUCCESS]
[ODBC][17658][1485155828.125084][SQLAllocHandle.c][364]
  Entry:
   Handle Type = 2
   Input Handle = 0x1a21380
[ODBC][17658][1485155828.127630][SQLAllocHandle.c][482]
  Exit:[SQL_SUCCESS]
   Output Handle = 0x1a17890
[ODBC][17658][1485155828.130371][SQLDriverConnectW.c][286]
  Entry:            
   Connection = 0x1a17890            
   Window Hdl = (nil)            
   Str In = [UID=sa;DATABASE=...;TDS_Version=8.0;DRIVER=FreeTDS;PWD=.....;SERVER=.....;ClientCharset=UTF8;PORT=...;][length = 114]            
   Str Out = (nil)            
   Str Out Max = 0            
   Str Out Ptr = (nil)            
   Completion = 0
  UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE 'UCS-2LE'

[ODBC][17658][1485155829.140224][SQLDriverConnectW.c][777]
  Exit:[SQL_ERROR]
[ODBC][17658][1485155829.143848][SQLDriverConnect.c][687]
  Entry:            
   Connection = 0x1a17890            
   Window Hdl = (nil)            
   Str In = [UID=sa;DATABASE=...;TDS_Version=8.0;DRIVER=FreeTDS;PWD=**********;SERVER=......;ClientCharset=UTF8;PORT=....;][length = 114 (SQL_NTS)]            
   Str Out = 0x7ffc3876ab00            
   Str Out Max = 2048            
   Str Out Ptr = (nil)            
   Completion = 0
  UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE 'UCS-2LE'

  DIAG [08S01] [FreeTDS][SQL Server]Unable to connect: Adaptive Server is unavailable or does not exist

  DIAG [08001] [FreeTDS][SQL Server]Unable to connect to data source

[ODBC][17658][1485155830.154707][SQLDriverConnect.c][1273]
  Exit:[SQL_ERROR]
[ODBC][17658][1485155830.157808][SQLGetDiagRec.c][661]
  Entry:                
   Connection = 0x1a17890                
   Rec Number = 1                
   SQLState = 0x7ffc3876b6e0                
   Native = 0x7ffc3876b6cc                
   Message Text = 0x7ffc3876b6f0                
   Buffer Length = 1023                
   Text Len Ptr = 0x7ffc3876b6ca
[ODBC][17658][1485155830.160870][SQLGetDiagRec.c][698]
  Exit:[SQL_SUCCESS]                    
   SQLState = 08S01                    
   Native = 0x7ffc3876b6cc -> 20009                    
   Message Text = [[unixODBC][FreeTDS][SQL Server]Unable to connect: Adaptive Server is unavailable or does not exist]
[ODBC][17658][1485155830.163884][SQLFreeHandle.c][279]
  Entry:
   Handle Type = 2
   Input Handle = 0x1a17890
[ODBC][17658][1485155830.173285][SQLFreeHandle.c][330]
  Exit:[SQL_SUCCESS]




odbcinst.ini

[ODBC]
Trace= Yes
TraceFile = /home/gamesale/odbc.log

[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
UsageCount = 1

freetds.conf
[global]
tds version = 8.0
host = ....
port = ...
text size = 64512
try server login = yes
try domain login = no
client charset = UTF-8

odbc.ini

[srv-db4]
Description=srv-db4
Driver=FreeTDS
Servername=srv-db4


test.py

odbc.iniimport os
import pyodbc

os.environ["ODBCSYSINI"] = "/home/gamesale"


try:
   cnx = pyodbc.connect(
  DRIVER = 'FreeTDS',
  TDS_Version =  '8.0',
  ClientCharset = 'UTF8',
  PORT = '...',
  SERVER = '....',
  DATABASE = '..',
  UID = '..',
  PWD = '.....'
)
   cursor = cnx.cursor()
   print ("SUCCESS")
except Exception as e:
    print ("Error: " + str(e))

1 个答案:

答案 0 :(得分:1)

你有免费帐户吗?免费帐户只能使用http(s)连接到PythonAnywhere以外的特定白名单网站。请参阅herehere