在循环中使用UPDATE [英] Use UPDATE in a loop

查看:111
本文介绍了在循环中使用UPDATE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行以下代码时:

for acc_row in cursor.execute("select * from tabela1"):
    cursor.execute("UPDATE table_name SET column_name=? where column_name=?", ('some_value','some_value'))

我在引用包含for循环的行时收到以下错误:

I receive the following error referencing the line containing the for loop:

ProgrammingError: No results. Previous SQL was not a query

在没有更新语句的情况下,for循环可以正常工作,反之亦然

The for loop works fine without the update statement, and vice versa

软件:

Python 2.7
Windows
pyodbc 3.0.7
用于MS Access 2010的AccessDatabaseEngine

Python 2.7
Windows
pyodbc 3.0.7
AccessDatabaseEngine for MS Access 2010

推荐答案

您依靠cursor.对象的状态来控制for循环,但是随后您正在修改该对象(通过执行UPDATE语句)在循环内.您将需要维护两个游标对象,一个用于控制循环,另一个用于执行UPDATE.

You are relying on the state of the cursor. object to control the for loop, but then you are modifying that object (by executing the UPDATE statement) inside the loop. You'll need to maintain two cursor objects, one to control the loop and another one to perform the UPDATEs.

这篇关于在循环中使用UPDATE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆