【问题标题】:pyodbc - unable to update multiple items in rowpyodbc - 无法更新行中的多个项目
【发布时间】:2012-03-01 17:06:49
【问题描述】:

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

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.

【问题讨论】:

    标签: python sql pyodbc


    【解决方案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)
    ...
    

    【讨论】:

    • 如果列名和值在列表中,如何处理?
    【解决方案2】:

    我最终自己解决了问题。

    我在以下循环中执行我的 updateQuery

    for row in cursor.execute(selectQuery)
    

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

    【讨论】:

      【解决方案3】:
      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']))
      

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

      【讨论】:

      • 你传入了什么数据值?
      • 数据['Iperf 11N TCP UL'],数据['Iperf 11N TCP DL'],数据['Iperf 11N UDP UL'],数据['Iperf 11N UDP DL'],数据[ 'HTTP DL'],data['HTTP UL'] 是浮点数,date 是一个日期时间对象,data['Notes'] 是一个字符串。我确实尝试将前 6 个值的 %s 更改为 %f,但仍然没有运气
      • data['Notes'] 中的值是否包含撇号?如果是这样,您需要删除它或用双撇号替换它。
      • data['Notes'] = "IPerf Test Bed 自动提交"
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2015-10-14
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多