我有一个SQLServer TSQL查询,它有多个INSERT语句,从非常基本到有些复杂不等。
此查询在SQLServer Management中工作。
当我使用pypyodbc包并运行脚本时,脚本将运行但不提交。我尝试过使用和不使用commit()函数。
但是--如果我在结尾指定SELECT语句,脚本将提交插入。
所以这一切都很好,因为它可以工作,但我在我所有脚本的末尾放了一个不适用的SELECT语句。
有没有人知道我如何在没有SELECT语句的情况下提交这些文件呢?我不想把查询分成多个查询。
谢谢!
def execute_query(self,
query,
tuple_of_query_parameters,
commit=False,
return_insert_id=False,
return_results=True):
self.open_cursor()
try:
self.connection_cursor.execute(query,
tuple_of_query_parameters)
result_set = None
if return_results:
if return_insert_id:
result_set = self.connection_cursor.fetchone()[0]
else:
result_set = self.connection_cursor.fetchall()
if commit:
self.connection_cursor.commit()
except pypyodbc.Error as e:
print('Check for "USE" in script!!!')
raise
finally:
self.close_cursor()
return result_set发布于 2018-09-24 13:10:16
试试这个:
self.connection_cursor.execute(query,
tuple_of_query_parameters)
if commit:
self.connection_cursor.commit() #put commit here, immediately after execute我想那会有效果的。
https://stackoverflow.com/questions/52480036
复制相似问题