且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用多个SQL语句更新数据库

更新时间:2023-02-08 16:03:43

对文档和帮助进行了长期研究之后,终于有了.我可以解决这个问题.

At-last after a long research on docs and help. I could able to solve the issue.

cursor.executemulti=True一起使用for循环有效.我不知道为什么我们需要遍历.

Using a for loop at cursor.execute with multi=True worked. I don't know why we need to loop through.

for result in cursor.execute(SQL, multi=True):
    pass

没有循环,只是cursor.execute(SQL, multi=True)在数据库中未做任何更改.

Without loop just cursor.execute(SQL, multi=True) did not do any changes in the database.

import mysql.connector

cnx = mysql.connector.connect(user='scott', database='test')
cursor = cnx.cursor()

SQL = '''
    update my_table 
    set 
    LAY = 'P6682'
    , BLK = 'P6682'
    , ANI = 'P6682'
    where
    Shot = 'SH01';

    update my_table 
    set 
    LAY = '1863'
    , BLK = '1863'
    , ANI = '1863'
    where
    Shot = 'SH02'
'''

for result in cursor.execute(SQL, multi=True):
    pass

cnx.commit()
cur.close()
cnx.close()
cnx.disconnect()