pyodbc - 无法更新行中的多个项目

时间:2012-03-01 17:06:49

标签: python sql pyodbc

我正在尝试连续更新多个项目,我的代码如下:

updateQuery = "UPDATE Throughput SET Test_Date = '%s' ,  Iperf11ntcpUL = '%s' , Iperf11ntcpDL = '%s' , Iperf11nudpUL = '%s' , Iperf11nudpDL = '%s' , HttpDL = '%s' , HttpUL = '%s', notes2='%s' where DeviceName = '%s' and Buildinfo ='%s' and Band = '%s' and Buildtype = '%s' " %(date, data['Iperf 11N TCP UL'], data['Iperf 11N TCP DL'], data['Iperf 11N UDP UL'], data['Iperf 11N UDP DL'], data['HTTP DL'],data['HTTP UL'],data['Notes'], data['Device Name'], data['Build Info / No.'], data['Band'], data['Build Type'])

cursor.execute(updateQuery)

执行此代码时出现以下错误:

Previous SQL was not a query.

3 个答案:

答案 0 :(得分:1)

Parameterize更新查询:

...
update = """update Throughput
            set Test_Date = ?, Iperf11ntcpUL = ?, Iperf11ntcpDL = ?,  
                Iperf11nudpUL = ?, Iperf11nudpDL = ?, HttpDL = ?,
                HttpUL = ?, notes2 = ?
            where
                DeviceName = ? and Buildinfo = ? and
                Band = ? and Buildtype = ?;"""

parameters = [date, data['Iperf 11N TCP UL'], data['Iperf 11N TCP DL'],
              data['Iperf 11N UDP UL'], data['Iperf 11N UDP DL'],
              data['HTTP DL'], data['HTTP UL'], data['Notes'],
              data['Device Name'], data['Build Info / No.'],
              data['Band'], data['Build Type']]

cursor.execute(update, parameters)
...

答案 1 :(得分:1)

我最终自己搞清了这个问题。

我在以下循环中执行 updateQuery

for row in cursor.execute(selectQuery)

相反,我抓住了行中的 id ,退出循环后,我为获取的id执行了更新语句

答案 2 :(得分:0)

updateQuery = "UPDATE Throughput SET Test_Date = '%s' ,  Iperf11ntcpUL = '%s' , Iperf11ntcpDL = '%s' , Iperf11nudpUL = '%s' , Iperf11nudpDL = '%s' , HttpDL = '%s' , HttpUL = '%s', notes2='%s' where DeviceName = '%s' and Buildinfo ='%s' and Band = '%s' and Buildtype = '%s' " %((date, data['Iperf 11N TCP UL'], data['Iperf 11N TCP DL'], data['Iperf 11N UDP UL'], data['Iperf 11N UDP DL'], data['HTTP DL'],data['HTTP UL'],data['Notes'], data['Device Name'], data['Build Info / No.'], data['Band'], data['Build Type']))

查询中的'%'之后的所有内容(放置值的位置)都应该是一个数组。只需将这些值包装在括号中(参见上文)