pymssql的cursor.executemany()的执行顺序

Execution Order of pymssql's cursor.executemany()

pymssql模块的cursor.executemany(...)是否有保证的执行顺序?

import pymssql

# Example retrieved from: http://pymssql.org/en/stable/pymssql_examples.html

# ...
conn = pymssql.connect(server, user, password, "tempdb")
cursor = conn.cursor()

# ...
cursor.executemany(
    "INSERT INTO persons VALUES (%d, %s, %s)",
    [(1, 'John Smith', 'John Doe'),
     (2, 'Jane Doe', 'Joe Dog'),
     (3, 'Mike T.', 'Sarah H.')])
conn.commit()
conn.close()

另请参阅:http://pymssql.org/en/stable/pymssql_examples.html

在实际场景中,我需要按特定顺序更新值(我有一个有序的元组数组)并且希望避免使用 cursor.execute(...).[=20= 一个一个地执行这些更新]


看起来 PEP 249 对其要求非常开放...

Prepare a database operation (query or command) and then execute it against all parameter sequences or mappings found in the sequence seq_of_parameters.

Modules are free to implement this method using multiple calls to the .execute() method or by using array operations to have the database process the sequence as a whole in one call.

https://www.python.org/dev/peps/pep-0249/#executemany

这引发了另一个问题... pymssql 对 PEP 249 的实施是否无论如何都会用 cursor.execute(...) 一个接一个地执行它们?

def executemany(self, operation, params_seq):
    self.description = None
    rownumber = 0
    for params in params_seq:
        self.execute(operation, params)
        # support correct rowcount across multiple executes
        rownumber += self._rownumber
    self._rownumber = rownumber

根据源代码,executemany函数只是迭代给定的序列并调用execute

参考:https://github.com/pymssql/pymssql/blob/891b20e29e4e247c17b202e8e34e5c739b6090ef/src/pymssql.pyx#L472