使用 django 连接到旧版 informix 数据库

Posted

技术标签:

【中文标题】使用 django 连接到旧版 informix 数据库【英文标题】:Connecting to a legacy informix database with django 【发布时间】:2018-08-01 13:22:12 【问题描述】:

我正在尝试使用 Django 连接到现有的 Informix 数据库,我正在使用 django-pyodbc-azure 通过 odbc 处理连接,在 python 解释器中尝试时连接工作正常:

>>>import pyodbc 
>>>conn = pyodbc.connect('DSN=test_ifx;UID=test;PWD=test')
>>>curs = conn.cursor()
>>>curs.execute("select * from someTable")
<pyodbc.Cursor object at 0x0052E520>

所以这项工作很好,但在 django 中我创建了这样的数据库连接参数:

DATABASES = 
    'default' : 
        'ENGINE' : 'sql_server.pyodbc',
        'NAME' : 'test',
        'USERNAME' : 'test',
        'PASSWORD' : 'test',
        'PORT' : '1260',
        'OPTIONS':
            'dsn' : 'test_ifx'
        
    

所以当我启动服务器时,我收到以下错误:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0353DED0>
Traceback (most recent call last):
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection
    self.connect()
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\base.py", line 321, in get_new_connection
    conn.timeout = query_timeout
pyodbc.Error: ('HYC00', '[HYC00] [Informix][Informix ODBC Driver]Driver not capable. (-11092) (SQLSetConnectAttr)')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\runserver.py", line 123, in inner_run
    self.check_migrations()
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 427, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
    self.build_graph()
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\loader.py", line 207, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\recorder.py", line 61, in applied_migrations
    if self.has_table():
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\recorder.py", line 44, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 255, in cursor
    return self._cursor()
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 232, in _cursor
    self.ensure_connection()
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection
    self.connect()
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection
    self.connect()
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\PatrickStewball\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\base.py", line 321, in get_new_connection
    conn.timeout = query_timeout
django.db.utils.Error: ('HYC00', '[HYC00] [Informix][Informix ODBC Driver]Driver not capable. (-11092) (SQLSetConnectAttr)')

我的电脑在 64 位 windows 7 上运行,带有 informix 数据库的服务器在我的本地网络上。任何帮助都会很棒。

编辑:ODBC 跟踪

所以我在 odbc 跟踪中确实有 DIAG [01S00] [Informix][Informix ODBC Driver]Invalid connection string attribute. (-11005) 错误,现在的问题是如何从正在工作的 pyodbc.connect('DSN=test_ifx;UID=test;PWD=test') 转到 Django DATABASES 参数

python.exe mana 520-1df4    ENTER SQLSetEnvAttr 
        SQLHENV             0x00000000
        SQLINTEGER                 201 <SQL_ATTR_CONNECTION_POOLING>
        SQLPOINTER                 2 <SQL_CP_ONE_PER_HENV>
        SQLINTEGER                   4 

python.exe mana 520-1df4    EXIT  SQLSetEnvAttr  with return code 0 (SQL_SUCCESS)
        SQLHENV             0x00000000
        SQLINTEGER                 201 <SQL_ATTR_CONNECTION_POOLING>
        SQLPOINTER                 2 <SQL_CP_ONE_PER_HENV>
        SQLINTEGER                   4 

python.exe mana 520-1df4    ENTER SQLAllocHandle 
        SQLSMALLINT                  1 <SQL_HANDLE_ENV>
        SQLHANDLE           0x00000000
        SQLHANDLE *         0x7278D8A4

python.exe mana 520-1df4    EXIT  SQLAllocHandle  with return code 0 (SQL_SUCCESS)
        SQLSMALLINT                  1 <SQL_HANDLE_ENV>
        SQLHANDLE           0x00000000
        SQLHANDLE *         0x7278D8A4 ( 0x00124248)

python.exe mana 520-1df4    ENTER SQLSetEnvAttr 
        SQLHENV             0x00124248
        SQLINTEGER                 200 <SQL_ATTR_ODBC_VERSION>
        SQLPOINTER                 3 <SQL_OV_ODBC3>
        SQLINTEGER                   4 

python.exe mana 520-1df4    EXIT  SQLSetEnvAttr  with return code 0 (SQL_SUCCESS)
        SQLHENV             0x00124248
        SQLINTEGER                 200 <SQL_ATTR_ODBC_VERSION>
        SQLPOINTER                 3 <SQL_OV_ODBC3>
        SQLINTEGER                   4 

python.exe mana 520-1df4    ENTER SQLAllocHandle 
        SQLSMALLINT                  2 <SQL_HANDLE_DBC>
        SQLHANDLE           0x00124248
        SQLHANDLE *         0x03C1E4B4

python.exe mana 520-1df4    EXIT  SQLAllocHandle  with return code 0 (SQL_SUCCESS)
        SQLSMALLINT                  2 <SQL_HANDLE_DBC>
        SQLHANDLE           0x00124248
        SQLHANDLE *         0x03C1E4B4 ( 0x001242C0)

python.exe mana 520-1df4    ENTER SQLDriverConnectW 
        HDBC                0x001242C0
        HWND                0x00000000
        WCHAR *             0x70048B34 [      -3] "******\ 0"
        SWORD                       -3 
        WCHAR *             0x70048B34 
        SWORD                       -3 
        SWORD *             0x00000000
        UWORD                        0 <SQL_DRIVER_NOPROMPT>

python.exe mana 520-1df4    EXIT  SQLDriverConnectW  with return code 1 (SQL_SUCCESS_WITH_INFO)
        HDBC                0x001242C0
        HWND                0x00000000
        WCHAR *             0x70048B34 [      -3] "******\ 0"
        SWORD                       -3 
        WCHAR *             0x70048B34 <Invalid buffer length!> [-3]
        SWORD                       -3 
        SWORD *             0x00000000
        UWORD                        0 <SQL_DRIVER_NOPROMPT>

        DIAG [01S00] [Informix][Informix ODBC Driver]Invalid connection string attribute. (-11005) 

        DIAG [01S00] [Informix][Informix ODBC Driver]Invalid connection string attribute. (-11005) 

        DIAG [01S00] [Informix][Informix ODBC Driver]Invalid connection string attribute. (-11005) 

python.exe mana 520-1df4    ENTER SQLSetConnectAttr 
        SQLHDBC             0x001242C0
        SQLINTEGER                 102 <SQL_ATTR_AUTOCOMMIT>
        SQLPOINTER                 0 <SQL_AUTOCOMMIT_OFF>
        SQLINTEGER                  -5 

python.exe mana 520-1df4    EXIT  SQLSetConnectAttr  with return code 1 (SQL_SUCCESS_WITH_INFO)
        SQLHDBC             0x001242C0
        SQLINTEGER                 102 <SQL_ATTR_AUTOCOMMIT>
        SQLPOINTER                 0 <SQL_AUTOCOMMIT_OFF>
        SQLINTEGER                  -5 

        DIAG [01S02] [Informix][Informix ODBC Driver]Option value changed. (-11007) 

python.exe mana 520-1df4    ENTER SQLGetInfoW 
        HDBC                0x001242C0
        UWORD                       77 <SQL_DRIVER_ODBC_VER>
        PTR                 0x0400F9C8 
        SWORD                       40 
        SWORD *             0x03C1E434

python.exe mana 520-1df4    EXIT  SQLGetInfoW  with return code 0 (SQL_SUCCESS)
        HDBC                0x001242C0
        UWORD                       77 <SQL_DRIVER_ODBC_VER>
        PTR                 0x0400F9C8 [      10] "03.00"
        SWORD                       40 
        SWORD *             0x03C1E434 (10)

python.exe mana 520-1df4    ENTER SQLGetInfoW 
        HDBC                0x001242C0
        UWORD                    10002 <SQL_DESCRIBE_PARAMETER>
        PTR                 0x04000818 
        SWORD                        4 
        SWORD *             0x03C1E434

python.exe mana 520-1df4    EXIT  SQLGetInfoW  with return code 0 (SQL_SUCCESS)
        HDBC                0x001242C0
        UWORD                    10002 <SQL_DESCRIBE_PARAMETER>
        PTR                 0x04000818 [       2] "N"
        SWORD                        4 
        SWORD *             0x03C1E434 (2)

python.exe mana 520-1df4    ENTER SQLGetInfoW 
        HDBC                0x001242C0
        UWORD                      111 <SQL_NEED_LONG_DATA_LEN>
        PTR                 0x04000818 
        SWORD                        4 
        SWORD *             0x03C1E434

python.exe mana 520-1df4    EXIT  SQLGetInfoW  with return code 0 (SQL_SUCCESS)
        HDBC                0x001242C0
        UWORD                      111 <SQL_NEED_LONG_DATA_LEN>
        PTR                 0x04000818 [       2] "Y"
        SWORD                        4 
        SWORD *             0x03C1E434 (2)

python.exe mana 520-1df4    ENTER SQLAllocHandle 
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x001242C0
        SQLHANDLE *         0x03C1E40C

python.exe mana 520-1df4    EXIT  SQLAllocHandle  with return code 0 (SQL_SUCCESS)
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x001242C0
        SQLHANDLE *         0x03C1E40C ( 0x0400CEB0)

python.exe mana 520-1df4    ENTER SQLGetTypeInfo 
        HSTMT               0x0400CEB0
        SWORD                       12 <SQL_VARCHAR>

python.exe mana 520-1df4    EXIT  SQLGetTypeInfo  with return code 0 (SQL_SUCCESS)
        HSTMT               0x0400CEB0
        SWORD                       12 <SQL_VARCHAR>

python.exe mana 520-1df4    ENTER SQLFetch 
        HSTMT               0x0400CEB0

python.exe mana 520-1df4    EXIT  SQLFetch  with return code 100 (SQL_NO_DATA_FOUND)
        HSTMT               0x0400CEB0

python.exe mana 520-1df4    ENTER SQLFreeStmt 
        HSTMT               0x0400CEB0
        UWORD                        0 <SQL_CLOSE>

python.exe mana 520-1df4    EXIT  SQLFreeStmt  with return code 0 (SQL_SUCCESS)
        HSTMT               0x0400CEB0
        UWORD                        0 <SQL_CLOSE>

python.exe mana 520-1df4    ENTER SQLFreeHandle 
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x0400CEB0

python.exe mana 520-1df4    EXIT  SQLFreeHandle  with return code 0 (SQL_SUCCESS)
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x0400CEB0

python.exe mana 520-1df4    ENTER SQLAllocHandle 
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x001242C0
        SQLHANDLE *         0x03C1E408

python.exe mana 520-1df4    EXIT  SQLAllocHandle  with return code 0 (SQL_SUCCESS)
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x001242C0
        SQLHANDLE *         0x03C1E408 ( 0x0400CEB0)

python.exe mana 520-1df4    ENTER SQLGetTypeInfo 
        HSTMT               0x0400CEB0
        SWORD                       -9 <SQL_WVARCHAR>

python.exe mana 520-1df4    EXIT  SQLGetTypeInfo  with return code -1 (SQL_ERROR)
        HSTMT               0x0400CEB0
        SWORD                       -9 <SQL_WVARCHAR>

        DIAG [HY004] [Informix][Informix ODBC Driver]SQL data type out of range. (-11064) 

python.exe mana 520-1df4    ENTER SQLFreeStmt 
        HSTMT               0x0400CEB0
        UWORD                        0 <SQL_CLOSE>

python.exe mana 520-1df4    EXIT  SQLFreeStmt  with return code 0 (SQL_SUCCESS)
        HSTMT               0x0400CEB0
        UWORD                        0 <SQL_CLOSE>

python.exe mana 520-1df4    ENTER SQLFreeHandle 
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x0400CEB0

python.exe mana 520-1df4    EXIT  SQLFreeHandle  with return code 0 (SQL_SUCCESS)
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x0400CEB0

python.exe mana 520-1df4    ENTER SQLAllocHandle 
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x001242C0
        SQLHANDLE *         0x03C1E404

python.exe mana 520-1df4    EXIT  SQLAllocHandle  with return code 0 (SQL_SUCCESS)
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x001242C0
        SQLHANDLE *         0x03C1E404 ( 0x0400CEB0)

python.exe mana 520-1df4    ENTER SQLGetTypeInfo 
        HSTMT               0x0400CEB0
        SWORD                       -3 <SQL_VARBINARY>

python.exe mana 520-1df4    EXIT  SQLGetTypeInfo  with return code 0 (SQL_SUCCESS)
        HSTMT               0x0400CEB0
        SWORD                       -3 <SQL_VARBINARY>

python.exe mana 520-1df4    ENTER SQLFetch 
        HSTMT               0x0400CEB0

python.exe mana 520-1df4    EXIT  SQLFetch  with return code 100 (SQL_NO_DATA_FOUND)
        HSTMT               0x0400CEB0

python.exe mana 520-1df4    ENTER SQLFreeStmt 
        HSTMT               0x0400CEB0
        UWORD                        0 <SQL_CLOSE>

python.exe mana 520-1df4    EXIT  SQLFreeStmt  with return code 0 (SQL_SUCCESS)
        HSTMT               0x0400CEB0
        UWORD                        0 <SQL_CLOSE>

python.exe mana 520-1df4    ENTER SQLFreeHandle 
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x0400CEB0

python.exe mana 520-1df4    EXIT  SQLFreeHandle  with return code 0 (SQL_SUCCESS)
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x0400CEB0

python.exe mana 520-1df4    ENTER SQLAllocHandle 
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x001242C0
        SQLHANDLE *         0x03C1E400

python.exe mana 520-1df4    EXIT  SQLAllocHandle  with return code 0 (SQL_SUCCESS)
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x001242C0
        SQLHANDLE *         0x03C1E400 ( 0x0400CEB0)

python.exe mana 520-1df4    ENTER SQLGetTypeInfo 
        HSTMT               0x0400CEB0
        SWORD                       93 <SQL_TYPE_TIMESTAMP>

python.exe mana 520-1df4    EXIT  SQLGetTypeInfo  with return code 0 (SQL_SUCCESS)
        HSTMT               0x0400CEB0
        SWORD                       93 <SQL_TYPE_TIMESTAMP>

python.exe mana 520-1df4    ENTER SQLFetch 
        HSTMT               0x0400CEB0

python.exe mana 520-1df4    EXIT  SQLFetch  with return code 0 (SQL_SUCCESS)
        HSTMT               0x0400CEB0

python.exe mana 520-1df4    ENTER SQLGetData 
        HSTMT               0x0400CEB0
        UWORD                        3 
        SWORD                        4 <SQL_C_LONG>
        PTR                 <unknown type>
        SQLLEN                     4
        SQLLEN *            0x00000000

python.exe mana 520-1df4    EXIT  SQLGetData  with return code 0 (SQL_SUCCESS)
        HSTMT               0x0400CEB0
        UWORD                        3 
        SWORD                        4 <SQL_C_LONG>
        PTR                 <unknown type>
        SQLLEN                     4
        SQLLEN *            0x00000000

python.exe mana 520-1df4    ENTER SQLFreeStmt 
        HSTMT               0x0400CEB0
        UWORD                        0 <SQL_CLOSE>

python.exe mana 520-1df4    EXIT  SQLFreeStmt  with return code 0 (SQL_SUCCESS)
        HSTMT               0x0400CEB0
        UWORD                        0 <SQL_CLOSE>

python.exe mana 520-1df4    ENTER SQLFreeHandle 
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x0400CEB0

python.exe mana 520-1df4    EXIT  SQLFreeHandle  with return code 0 (SQL_SUCCESS)
        SQLSMALLINT                  3 <SQL_HANDLE_STMT>
        SQLHANDLE           0x0400CEB0

python.exe mana 520-1df4    ENTER SQLSetConnectAttr 
        SQLHDBC             0x001242C0
        SQLINTEGER                 113 <SQL_ATTR_CONNECTION_TIMEOUT>
        SQLPOINTER                 0
        SQLINTEGER                  -5 

python.exe mana 520-1df4    EXIT  SQLSetConnectAttr  with return code -1 (SQL_ERROR)
        SQLHDBC             0x001242C0
        SQLINTEGER                 113 <SQL_ATTR_CONNECTION_TIMEOUT>
        SQLPOINTER                 0
        SQLINTEGER                  -5 

        DIAG [HYC00] [Informix][Informix ODBC Driver]Driver not capable. (-11092) 

python.exe mana 520-1df4    ENTER SQLGetDiagRecW 
        SQLSMALLINT                  2 <SQL_HANDLE_DBC>
        SQLHANDLE           0x001242C0
        SQLSMALLINT                  1 
        SQLWCHAR *          0x03C1E4FC
        SQLINTEGER *        0x03C1DCD8
        SQLWCHAR *          0x03C1DCFC 
        SQLSMALLINT               1023 
        SQLSMALLINT *       0x03C1DCDC

python.exe mana 520-1df4    EXIT  SQLGetDiagRecW  with return code 0 (SQL_SUCCESS)
        SQLSMALLINT                  2 <SQL_HANDLE_DBC>
        SQLHANDLE           0x001242C0
        SQLSMALLINT                  1 
        SQLWCHAR *          0x03C1E4FC [       5] "HYC00"
        SQLINTEGER *        0x03C1DCD8 (-11092)
        SQLWCHAR *          0x03C1DCFC [      51] "[Informix][Informix ODBC Driver]Driver not capable."
        SQLSMALLINT               1023 
        SQLSMALLINT *       0x03C1DCDC (51)

python.exe mana 520-1df4    ENTER SQLGetDiagRecW 
        SQLSMALLINT                  2 <SQL_HANDLE_DBC>
        SQLHANDLE           0x001242C0
        SQLSMALLINT                  2 
        SQLWCHAR *          0x03C1E4FC
        SQLINTEGER *        0x03C1DCD8
        SQLWCHAR *          0x03C1DCFC 
        SQLSMALLINT               1023 
        SQLSMALLINT *       0x03C1DCDC

python.exe mana 520-1df4    EXIT  SQLGetDiagRecW  with return code 100 (SQL_NO_DATA_FOUND)
        SQLSMALLINT                  2 <SQL_HANDLE_DBC>
        SQLHANDLE           0x001242C0
        SQLSMALLINT                  2 
        SQLWCHAR *          0x03C1E4FC
        SQLINTEGER *        0x03C1DCD8
        SQLWCHAR *          0x03C1DCFC 
        SQLSMALLINT               1023 
        SQLSMALLINT *       0x03C1DCDC

python.exe mana 520-294c    ENTER SQLEndTran 
        SQLSMALLINT                  2 <SQL_HANDLE_DBC>
        SQLHANDLE           0x001242C0
        SQLSMALLINT                  1 

python.exe mana 520-294c    EXIT  SQLEndTran  with return code -1 (SQL_ERROR)
        SQLSMALLINT                  2 <SQL_HANDLE_DBC>
        SQLHANDLE           0x001242C0
        SQLSMALLINT                  1 

        DIAG [HYC00] [Informix][Informix ODBC Driver]Driver not capable. (-11092) 

python.exe mana 520-294c    ENTER SQLDisconnect 
        HDBC                0x001242C0

python.exe mana 520-294c    EXIT  SQLDisconnect  with return code -1 (SQL_ERROR)
        HDBC                0x001242C0

        DIAG [25000] [Microsoft][Gestionnaire de pilotes ODBC] État de la transaction non valide (0) 

python.exe mana 520-294c    ENTER SQLFreeHandle 
        SQLSMALLINT                  2 <SQL_HANDLE_DBC>
        SQLHANDLE           0x001242C0

python.exe mana 520-294c    EXIT  SQLFreeHandle  with return code -1 (SQL_ERROR)
        SQLSMALLINT                  2 <SQL_HANDLE_DBC>
        SQLHANDLE           0x001242C0

        DIAG [HY010] [Microsoft][Gestionnaire de pilotes ODBC] Erreur de séquence de la fonction (0) 

【问题讨论】:

"驱动程序不支持。(-11092) (SQLSetConnectAttr)" ..... 为什么不获取 ODBC 跟踪以查看 Django 尝试设置的参数(并且不被 Informix ODBC 驱动程序接受) )。那可能会发出更多的光;) 感谢您的帮助,我已经添加了 odbc 跟踪。我想我在 django 中没有合适的数据库参数。 Informix 不支持 SQL_ATTR_CONNECTION_TIMEOUT。查看 sql_server\pyodbc\base.py 和 pyodbc-master\src\connect.cpp 的代码,只有在 connection_timeout django 属性大于 0 时才会调用 SQLSetConnectAttr(SQL_ATTR_LOGIN_TIMEOUT)。你是否设置了它? @J_S 我根本没有设置属性,我在数据库选项参数中添加了以下选项以确保但没有改进'OPTIONS': 'dsn' : 'test_ifx', 'connect_timeout': 0, 'timeout' : 0, 让我再检查一下代码,你可以在连接前使用SQL_ATTR_CONNECTION_TIMEOUT,但不能在连接后使用,如果在你得到“驱动程序不支持”后设置 【参考方案1】:

如果不进行一些重写,django-pyodbc-azure 将无法与 Informix ODBC 一起使用。 “驱动程序不支持”错误是由pyodbc中的以下代码引起的:

    unicode_results = options.get('unicode_results', False)
    timeout = options.get('connection_timeout', 0)
    retries = options.get('connection_retries', 5)
    backoff_time = options.get('connection_retry_backoff_time', 5)
    query_timeout = options.get('query_timeout', 0)

    conn = None
    retry_count = 0
    need_to_retry = False
    while conn is None:
        try:
            conn = Database.connect(connstr,
                                    unicode_results=unicode_results,
                                    timeout=timeout)
        except Exception as e:
            for error_number in self._transient_error_numbers:
                if error_number in e.args[1]:
                    if error_number in e.args[1] and retry_count < retries:
                        time.sleep(backoff_time)
                        need_to_retry = True
                        retry_count = retry_count + 1
                    else:
                        need_to_retry = False
                    break
            if not need_to_retry:
                raise

 #   conn.timeout = query_timeout
    return conn

“conn.timeout = query_timeout”会导致 SQLSetConnectAtt() 调用,因为它会在建立连接后尝试设置属性。这也没有多大意义,因为 query_timeout 和连接超时是两个不同的东西。

无论如何,即使您修复了该问题(注释会解决),sql_server.pyodbc 代码也有一些特定于 SQLServer 的 SQL。 Informix 无法理解。

django 失败:

    raise dj_exc_value.with_traceback(traceback) from exc_value
File "D:\infx\Python36-32\lib\site-packages\django_pyodbc_azure-2.0.6.1-py3.6.egg\sql_server\pyodbc\base.py", line 462, in _set_autocommit
File "D:\infx\Python36-32\lib\site-packages\django_pyodbc_azure-2.0.6.1-py3.6.egg\sql_server\pyodbc\base.py", line 424, in _get_trancount
django.db.utils.ProgrammingError: ('42000', '[42000] [Informix][Informix ODBC Driver][Informix]A syntax error has occurred. (-201) (SQLExecDirectW)')

因为它试图执行:

    python.exe mana 1588-2630   ENTER SQLExecDirectW 
    HSTMT               0x0471EB20
    WCHAR *             0x03F55420 [      18] "SELECT @@TRANCOUNT"
    SDWORD                    18

    python.exe mana 1588-2630   EXIT  SQLExecDirectW  with return code -1 (SQL_ERROR)
    HSTMT               0x0471EB20
    WCHAR *             0x03F55420 [      18] "SELECT @@TRANCOUNT"
    SDWORD                    18

    DIAG [42000] [Informix][Informix ODBC Driver][Informix]A syntax error has occurred. (-201) 

“base.py”中的 SQLServer sql 语句并不多,因此您可能想尝试为 Informix 重写这些语句,但我不能 100% 确定它会起作用。 不过,一秒钟后,这并不是那么简单。 “introspection.py”充满了 SQLServer 特定的 SQL。

【讨论】:

感谢您的回答,我不会去更改 pyodbc,因为它听起来很乏味并且无法保证成功。你有什么不涉及pyodbc连接informix数据库的想法吗? 嗯。我猜你想要一些与 Django 兼容的东西,对吧? IfxPy 它是 Informix 特有的模块,但没有 django 的模块。

以上是关于使用 django 连接到旧版 informix 数据库的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 Hibernate 连接到 Informix

使用 .NET 连接到 Informix

在 Windows 上使用 pyodbc 连接到 Informix

使用 SQL Management Studio 连接到 Informix 数据库

使用 python 的 ibm_db 连接到 informix

通过 ip:port 使用 Perl DBI 连接到 Informix