请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

4-6 commit的context

4-6这里把commit放到with conn的context里是否更合理一些呢?

    with conn:
        with conn.cursor() as cursor:
            sql = "update school_student_info set stu_name='Bruce Lee' where stu_no=1001;"
            cursor.execute(sql)
        conn.commit()
        print("Finished Update")

如果cursor里有多条sql,把commit放到cursor里会反复写入,会降低效率吧

正在回答

1回答

如果cursor中需要执行多条SQL,也是在多条SQL执行完之后再commit。当然,可以参考PPT中的那样(也就是你贴的代码),在cursor关闭之后再commit,更加安全。

1 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号