我有一个要基于功能更新的sql表 到目前为止,这是我的代码:
def read(conn):
cursor = conn.cursor()
#cursor2 = conn.cursor()
cursor.execute("SELECT All [id],[keyword],[result],[status],[createddate] FROM [Table1].[dbo].[aa]")
index = 1
cursor.execute("set nocount on")
for row in cursor:
s = row[1]
s = re.sub(r'[^\w\s]', '', s)
a=do_func(s)
if a:
cursor.execute("update [Table1].[dbo].[aa] set status = 'completed', result = 'True' where id ={}".format(index))
else:
cursor.execute("update [Table1].[dbo].[aa] set status = 'completed', result = 'False' where id ={}".format(index))
if index == 10:
break
index += 1
我得到pyodbc.ProgrammingError: No results. Previous SQL was not a query.
我添加了“ set nocount on”但没有解决,我也尝试制作第二个光标但也没有解决问题
答案 0 :(得分:1)
好的,请参见代码:您将需要拆分光标以进行选择和光标进行更新,您不能同时使用两者。更新后,您将需要提交。让我知道它是否有效。
def read(conn):
selectcursor = conn.cursor()
updatecursor = conn.cursor()
selectcursor.execute("SELECT [id],[keyword],[result],[status],[createddate] FROM [Table1].[dbo].[aa]")
index = 1
result = selectcursor.fetchall()
for row in result:
s = row[1]
s = re.sub(r'[^\w\s]', '', s)
a=do_func(s)
if a:
updatecursor.execute("update [Table1].[dbo].[aa] set status = 'completed', result = 'True' where id ={}".format(index))
updatecursor.commit()
else:
updatecursor.execute("update [Table1].[dbo].[aa] set status = 'completed', result = 'False' where id ={}".format(index))
updatecursor.commit()
if index == 10:
break
index += 1
selectcursor.close()
updatecursor.close()