Execute a multi-row query. query -- string, query to execute on server args Sequence of sequences or mappings, parameters to use with query. Returns long integer rows affected, if any. This method improves perfo
(self, query, args)
| 210 | return r |
| 211 | |
| 212 | def executemany(self, query, args): |
| 213 | |
| 214 | """Execute a multi-row query. |
| 215 | |
| 216 | query -- string, query to execute on server |
| 217 | |
| 218 | args |
| 219 | |
| 220 | Sequence of sequences or mappings, parameters to use with |
| 221 | query. |
| 222 | |
| 223 | Returns long integer rows affected, if any. |
| 224 | |
| 225 | This method improves performance on multiple-row INSERT and |
| 226 | REPLACE. Otherwise it is equivalent to looping over args with |
| 227 | execute(). |
| 228 | |
| 229 | """ |
| 230 | del self.messages[:] |
| 231 | db = self._get_db() |
| 232 | if not args: return |
| 233 | m = insert_values.search(query) |
| 234 | if not m: |
| 235 | r = 0 |
| 236 | for a in args: |
| 237 | r = r + self.execute(query, a) |
| 238 | return r |
| 239 | p = m.start(1) |
| 240 | e = m.end(1) |
| 241 | qv = m.group(1) |
| 242 | try: |
| 243 | q = [] |
| 244 | for a in args: |
| 245 | if isinstance(a, dict): |
| 246 | data = qv.format(**db.literal(a)) |
| 247 | elif isinstance(a, tuple) or isinstance(a, list): |
| 248 | data = qv.format( *db.literal(a) ) |
| 249 | else: |
| 250 | data = qv.format( db.literal(a) ) |
| 251 | q.append( data ) |
| 252 | except TypeError as msg: |
| 253 | if msg.args[0] in ("not enough arguments for format string", |
| 254 | "not all arguments converted"): |
| 255 | self.errorhandler(self, ProgrammingError, msg.args[0]) |
| 256 | else: |
| 257 | self.errorhandler(self, TypeError, msg) |
| 258 | except (SystemExit, KeyboardInterrupt): |
| 259 | raise |
| 260 | except: |
| 261 | exc, value, tb = sys.exc_info() |
| 262 | del tb |
| 263 | self.errorhandler(self, exc, value) |
| 264 | r = self._query('\n'.join([query[:p], ',\n'.join(q), query[e:]])) |
| 265 | if not self._defer_warnings: self._warning_check() |
| 266 | return r |
| 267 | |
| 268 | def callproc(self, procname, args=()): |
| 269 |