pyodbc不提交

2024-04-26 14:10:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个SQLServerTSQL查询,它有多个INSERT语句,从非常基本到有些复杂。你知道吗

此查询在SQLServerManagementStudio中工作。你知道吗

当我使用Python pyodbc包并运行脚本时,脚本运行但不提交。我尝试过使用和不使用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

Tags: ofself脚本executereturnif语句result
1条回答
网友
1楼 · 发布于 2024-04-26 14:10:35

试试这个:

self.connection_cursor.execute(query,
                                   tuple_of_query_parameters)
if commit:
    self.connection_cursor.commit() #put commit here, immediately after execute

我想那就行了。你知道吗

相关问题 更多 >