连接提交函数 Python

connection commit function Python

我正在阅读文档,发现您只需要在一个事务中提交一次?

以下算作一笔交易还是每个功能算作一笔交易?

def main():
    conn=pyodbc.connect(sqlconnectionstring) # Assume this connects to the database
    cursor = conn.cursor()

    function1()
    function2()

    conn.commit()

def function1():
    # does inserting here

def function2():
    # does inserting here and calls function 3
    function3()

def function 3():
    # does more inserting here
main()

conn.commit() 是否足以提交所有函数中的所有插入,或者我是否必须将 "conn" 变量作为参数传递并在每个函数中提交?

谢谢!

是的,这足以提交所有事务,因为像插入和删除这样的事务将全部在函数内部执行,直到一个失败然后你会找到旧行。

但是那一次提交会将数据库状态更改为最近的一次