首页 > 解决方案 > 调用pyodbc游标时出现线程错误

问题描述

从 pyodbc 调用 cursor.columns 时,我收到“TypeError:内置操作的参数类型错误”错误,将问题追溯到 threading.py -> currentThread 函数...

我已经在一台新的 HP Z2 台式电脑上安装了我的 python 环境,运行:Win10 企业 x64 Python 2.7.13 on win32 PyScripter 3.5.1.0 x86

尝试使用 pyodbc 模块将数据插入我的数据库时突然收到一个非常奇怪的错误。我的代码 -

class clsDataBaseWrapper():
    def __init__(self,ServerAddress='WIGIG-703\SQLEXPRESS',DataBase='QCT_Python',dBEn=1):
        if (dBEn) :
            self.DataBase=DataBase
            self.cnxn = pyodbc.connect("Driver={SQL Server}"+
                                        """
                                        ;Server={0};
                                        Database={1};
                                        Trusted_Connection=yes;""".format(ServerAddress,DataBase))
            self.cursor = self.cnxn.cursor()
        else : print "\n*** Be Aware - you are not writing data to the DataBase !!! ***"

    def InsertData(self,Table,Data,dBEn):
        if (dBEn) :
            columns = self.cursor.columns(table=Table, schema='dbo').fetchall()
            insertQuery  = "insert into {0} values ({1})".format(Table, ','.join('?' * len(columns)))
            try :
                self.cursor.execute(insertQuery, Data)
                self.cnxn.commit()
            except Exception,e : print 'could not insert data into DB - ',e
        else : pass

我收到的错误 -

C:\Python27\lib\threading.py:1151: RuntimeWarning: tp_compare didn't return -1 or -2 for exception
  return _active[_get_ident()]
Traceback (most recent call last):
  File "C:\Qpython\IsonM_TC2\Services_IsonM_TC2.py", line 335, in <module>
    test.InsertData('PLL_CL_PN_PLL',[],1)
  File "C:\Qpython\IsonM_TC2\Services_IsonM_TC2.py", line 87, in InsertData
    columns = columns.fetchall()
  File "C:\Qpython\IsonM_TC2\Services_IsonM_TC2.py", line 87, in InsertData
    columns = columns.fetchall()
  File "C:\Python27\lib\bdb.py", line 49, in trace_dispatch
    return self.dispatch_line(frame)
  File "C:\Python27\lib\bdb.py", line 67, in dispatch_line
    self.user_line(frame)
  File "<string>", line 126, in user_line
  File "C:\Python27\lib\threading.py", line 1151, in currentThread
    return _active[_get_ident()]
TypeError: bad argument type for built-in operation

这很奇怪,因为它在我运行相同环境的其他旧 PC 上运行良好。我在这里想念什么?尝试重新安装python,pyscripter ...

标签: pythonmultithreadingpyodbcberkeley-db

解决方案


推荐阅读