Execute a multi-row query. :param query: query to execute on server :param args: Sequence of sequences or mappings. It is used as parameter. :return: Number of rows affected, if any. This method improves performance on multiple-row INSERT and REPLACE. Othe
(self, query, args)
| 218 | return self._mogrify(query, args).decode(self._get_db().encoding) |
| 219 | |
| 220 | def executemany(self, query, args): |
| 221 | # type: (str, list) -> int |
| 222 | """Execute a multi-row query. |
| 223 | |
| 224 | :param query: query to execute on server |
| 225 | :param args: Sequence of sequences or mappings. It is used as parameter. |
| 226 | :return: Number of rows affected, if any. |
| 227 | |
| 228 | This method improves performance on multiple-row INSERT and |
| 229 | REPLACE. Otherwise it is equivalent to looping over args with |
| 230 | execute(). |
| 231 | """ |
| 232 | if not args: |
| 233 | return |
| 234 | |
| 235 | m = RE_INSERT_VALUES.match(query) |
| 236 | if m: |
| 237 | q_prefix = m.group(1) % () |
| 238 | q_values = m.group(2).rstrip() |
| 239 | q_postfix = m.group(3) or "" |
| 240 | assert q_values[0] == "(" and q_values[-1] == ")" |
| 241 | return self._do_execute_many( |
| 242 | q_prefix, |
| 243 | q_values, |
| 244 | q_postfix, |
| 245 | args, |
| 246 | self.max_stmt_length, |
| 247 | self._get_db().encoding, |
| 248 | ) |
| 249 | |
| 250 | self.rowcount = sum(self.execute(query, arg) for arg in args) |
| 251 | return self.rowcount |
| 252 | |
| 253 | def _do_execute_many( |
| 254 | self, prefix, values, postfix, args, max_stmt_length, encoding |