TypeError:“type”对象不能用pypyodbc(Python)订阅

2024-10-01 00:15:27 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的python代码有什么问题?在

我想连接到我的数据库,选择一些信息。这些都是在一个列表列表中,我抓取列表并执行“select..from..where..in”:

 import pypyodbc
 connection = pypyodbc.connect('Driver={SQL Server};'
                                    'Server=X;'
                                    'Database=Y;'
                                    'uid=X;pwd=Y')
 cursor = connection.cursor()

 NbFiche=0
 L=[[4702, 3095, 3543], [2040, 2030, 2020], []]
 for i in range(0,3):
    log=L[i]


    if (log is not None):
        if (len(log)==3):                

            SQLCommand = ("select count(*) from PRODUCTION where  ID_TV IN (?) ")
            cursor.execute(SQLCommand,(log,))
            results = cursor.fetchone()
            NbFiche += results[0]

这是错误:

^{pr2}$

新代码(编辑):

 import pypyodbc
connection = pypyodbc.connect('Driver={SQL Server};'
                              'Server=x;'
                                    'Database=y;'
                                    'uid=x;pwd=y')

cursor = connection.cursor()

NbFiche=0
L=[[4702, 3095, 3543], [2040, 2030, 2020]]
for log in L:
    log=tuple(log) # I also tried with a list

    SQLCommand = ("select count(*) from PRODUCTION where  ID_TV IN (?) ")
    cursor.execute(SQLCommand,(log,))
    results = cursor.fetchone()
    NbFiche += results[0]

编辑:

import pypyodbc
connection = pypyodbc.connect('Driver={SQL Server};'
                                  'Server=x;'
                                        'Database=y;'
                                        'uid=x;pwd=y')
cursor = connection.cursor()
NbFiche=0
L=[[4702, 3095], [2040, 2030, 2020]]
for log in L:
    SQLCommand = ("select count(*) from PRODUCTION where  ID_TV IN (?)")
    params = ','.join(map(str,log))
    cursor.execute(SQLCommand,params)
    results = cursor.fetchone()
    NbFiche += results[0]

结果如下:

Traceback (most recent call last):
  File "//Srvaktct-bur02/telecontact/TCT TRAVAIL/Pôle Fichier/AMIRA/STATISTIQUES/nimp.py", line 13, in <module>
    cursor.execute(SQLCommand,params)
  File "C:\Users\admin_fichier\AppData\Local\Programs\Python\Python35\lib\site-packages\pypyodbc-1.3.3-py3.5.egg\pypyodbc.py", line 1454, in execute
    raise TypeError("Params must be in a list, tuple, or Row")
TypeError: Params must be in a list, tuple, or Row

Tags: infromimportlog列表executeserverconnection
1条回答
网友
1楼 · 发布于 2024-10-01 00:15:27

我想是因为你有一张空名单:

L=[[4702, 3095, 3543], [2040, 2030, 2020], []]
                                           ^^

当使用if进行测试时,即使列表是空的,它也会通过测试,类似于下面的示例:

^{pr2}$

这实际上是错误的,因此,要解决这个问题,请按以下步骤操作:

>>> if l:
        print('l is not empty')
    else:
        print('l is Empty')


l is Empty

因此,将测试表达式更改为:

if log:
    if len(log)==3:
        #...

编辑:

如果log的长度是可变的,那么您可能必须首先构建字符串参数,然后将其传递给cursor.execute方法,方法如下:

SQLCommand = "select count(*) from PRODUCTION where ID_TV IN ({}) "
params = ','.join(map(str,log))
cursor.execute(SQLCommand.format(params))

编辑2:

如果参数是用户输入数据,前面提出的解决方案会暴露在SQL_injections漏洞中。因此,更好的方法是将它们作为参数传递给cursor.execute方法:

L=[[4702, 3095, 3543], [2040, 2030, 2020]]
for log in L:
    if log:
        SQLCommand = "select count(*) from PRODUCTION where  ID_TV IN ?"
        cursor.execute(SQLCommand, tuple(log))

如果有多个参数:

SQLCommand = "select ?, ? from PRODUCTION where ID_TV IN ?"
cursor.execute(SQLCommand, (p1, p2, tuple(log)))

相关问题 更多 >