pypyodbc 不提交
pypyodbc does not commit
我有一个 SQLServer TSQL 查询,它有多个 INSERT 语句,从非常基本到有些复杂。
此查询适用于 SQLServer Management Studio。
当我使用 Python pypyodbc 包和 运行 脚本时,脚本 运行s 但没有提交。我尝试过使用和不使用 commit() 函数。
BUT 如果我在末尾指定 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
试试这个:
self.connection_cursor.execute(query,
tuple_of_query_parameters)
if commit:
self.connection_cursor.commit() #put commit here, immediately after execute
我认为可以解决问题。
我有一个 SQLServer TSQL 查询,它有多个 INSERT 语句,从非常基本到有些复杂。
此查询适用于 SQLServer Management Studio。
当我使用 Python pypyodbc 包和 运行 脚本时,脚本 运行s 但没有提交。我尝试过使用和不使用 commit() 函数。
BUT 如果我在末尾指定 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
试试这个:
self.connection_cursor.execute(query,
tuple_of_query_parameters)
if commit:
self.connection_cursor.commit() #put commit here, immediately after execute
我认为可以解决问题。