我很难使用pyodbc
模块(python3
)。以下代码总是使 python 崩溃(我从 DOS 终端运行它)。main()
函数返回时发生崩溃。我的数据源是 4D v13 数据库远程服务器。我正在使用 4D-ODBC 驱动程序,该驱动程序似乎已正确安装但实际上不是有效的驱动程序。我已禁用 SSL 连接和我的防火墙。
class ODBCSource:
def __init__(self, dsn):
self.dsn = str(dsn)
try:
self.con = pyodbc.connect("dsn={}".format(self.dsn))
self.cur = self.con.cursor()
logger.info("ODBC Source DSN='{}' connected.".format(self.dsn))
except Exception as err:
self.con = None
self.cur = None
logger.error("Cannot connect ODBC Source DSN='{}': {}.".format(self.dsn, err))
def __bool__(self):
return not(self.con is None) or not(self.cur is None)
def __str__(self):
return "<cripython.dal.ODBCSource id={:#x} dsn='{}'>".format(id(self), self.dsn)
__repr__ = __str__
# Main Function:
def main():
db = ODBCSource('CRIPI-4D')
# Program Entry Point:
if(__name__ == "__main__"):
main()
Windows 崩溃窗口跟踪给我以下信息:
Problem signature:
Problem Event Name: APPCRASH
Application Name: python.exe
Application Version: 0.0.0.0
Application Timestamp: 5066b7a2
Fault Module Name: 4DODBC.dll
Fault Module Version: 14.0.1.0
Fault Module Timestamp: 53b4d1d6
Exception Code: c0000005
Exception Offset: 00031798
OS Version: 6.1.7601.2.1.0.256.48
Locale ID: 2060
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
我已启用 ODBC Trace 以检查代码可能有什么问题。我在日志中发现了 3 个错误。
[...]
odbcsource 1724-2a8 ENTER SQLGetTypeInfo
HSTMT 0x0059B1F0
SWORD 12 <SQL_VARCHAR>
odbcsource 1724-2a8 EXIT SQLGetTypeInfo with return code -1 (SQL_ERROR)
HSTMT 0x0059B1F0
SWORD 12 <SQL_VARCHAR>
DIAG [24000] [Microsoft][ODBC Driver Manager] Invalid cursor state (0)
odbcsource 1724-2a8 ENTER SQLGetTypeInfo
HSTMT 0x0059B1F0
SWORD -9 <SQL_WVARCHAR>
odbcsource 1724-2a8 EXIT SQLGetTypeInfo with return code -1 (SQL_ERROR)
HSTMT 0x0059B1F0
SWORD -9 <SQL_WVARCHAR>
DIAG [24000] [Microsoft][ODBC Driver Manager] Invalid cursor state (0)
odbcsource 1724-2a8 ENTER SQLGetTypeInfo
HSTMT 0x0059B1F0
SWORD -2 <SQL_BINARY>
odbcsource 1724-2a8 EXIT SQLGetTypeInfo with return code -1 (SQL_ERROR)
HSTMT 0x0059B1F0
SWORD -2 <SQL_BINARY>
DIAG [24000] [Microsoft][ODBC Driver Manager] Invalid cursor state (0)
odbcsource 1724-2a8 ENTER SQLFreeStmt
HSTMT 0x0059B1F0
UWORD 0 <SQL_CLOSE>
[...]
顺便说一句,当我从 Python IDLE 运行这个脚本时,我根本没有收到任何错误。有谁知道这段代码出了什么问题,我应该如何解决?