('SQL包含0个参数标记,但提供了50个参数','HY000')或TypeError:'tuple'对象不可调用

时间:2012-03-07 14:47:55

标签: python mysql

import pyodbc,nltk,array
cnxn = pyodbc.connect('Driver={MySQL ODBC 5.1 Driver};Server=127.0.0.1;Port=3306;Database=information_schema;User=root; Password=1234;Option=3;')
cursor = cnxn.cursor()
cursor.execute("use collegedatabase ;")
cursor.execute("select *  from sampledata ; ")
cnxn.commit()
s=[]
j=[]
x=[]
words = []
w = []
sfq = []
POS=[]
for entry in cursor:
        s.append(entry.injury_type),j.append(entry.injury_desc) 


from nltk.tokenize import PunktWordTokenizer
from nltk.corpus import stopwords
tokenizer = PunktWordTokenizer()
english_stops = set(stopwords.words('english'))
for i in range(0,26): # filter stop words
            tokenizer.tokenize(j[i])
            w.append([word for word in tokenizer.tokenize(j[i]) if word not in english_stops])

for a in range(0 , 26):#CONVERTING tokenzied text ito a string 
            sfq.append(" ".join(w[a])) 

from replacers import RegexpReplacer
replacer =  RegexpReplacer()
for a in range (0,26):#POS TAGGING
            replacer.replace(sfq[a])
            POS.append(tokenizer.tokenize(sfq[a]))                
            x.append(nltk.pos_tag(POS[a])) 
            cursor.executemany("update sampledata SET POS = %s where SRNO =%d",(x[a],a))

我得到的错误是:

Traceback (most recent call last):
  File "C:\Users\Vaibhav\Dropbox\DUE BY MONDAY\tryingtofixerror.py", line 35, in 
    cursor.executemany("update sampledata SET POS = %s where SRNO =%d",(x[a],a))
ProgrammingError: ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000')

在浏览找到问题的解决方案后,我想用这句话替换最后一行(第35行)

cursor.executemany((u'update sampledata SET POS = %s where SRNO =%d'.encode('utf-8'),(x[a],a))(u'MO-MK25'.encode('utf-8'))) 

我不知道我是否正在朝着正确的方向前进,但经历了这么多的网站和论坛,我得到了一个提示,可能是缩进或逗号错误。我不知道还能做什么。顺便说一句,生成的错误代码如下:

Traceback (most recent call last):
  File "C:\Users\Vaibhav\Dropbox\DUE BY MONDAY\tryingtofixerror.py", line 35, in 
    cursor.executemany((u'update sampledata SET POS = %s where SRNO =%d'.encode('utf-8'), (x[a],a)) (u'MO-MK25'.encode('utf-8')))
TypeError: 'tuple' object is not callable

1 个答案:

答案 0 :(得分:0)

方法executemany(sql, seq_of_parameters)对一组参数多次执行相同的SQL语句。因此,第二个参数seq_of_parameters必须是一系列参数元组,而不仅仅是一个参数元组:

cursor.executemany("update sampledata SET POS = ? where SRNO = ?", [(x[a], a)])

如果只传递一个元组,则光标将假定第一个项x[a]是参数元组。我猜这是一个由50个字符组成的字符串,被解释为50个参数的序列,而SQL字符串只需要2个。

此外,请注意我使用?作为占位符代码而不是%s,因为后者似乎不受PyODBC支持,因为它报告它预期有0个参数。

在您的情况下,您可能希望在循环中使用execute()方法,因为您只想在每次迭代时运行一次语句:

cursor.execute("update sampledata SET POS = ? where SRNO = ?", (x[a], a))