Execute an SQL command (or commands). This method can execute many SQL commands at once, when no arguments are provided. Example: .. code-block:: pycon >>> await con.execute(''' ... CREATE TABLE mytab (a int); ... INSERT
(
self,
query: str,
*args,
timeout: typing.Optional[float]=None,
)
| 318 | return self._protocol.is_in_transaction() |
| 319 | |
| 320 | async def execute( |
| 321 | self, |
| 322 | query: str, |
| 323 | *args, |
| 324 | timeout: typing.Optional[float]=None, |
| 325 | ) -> str: |
| 326 | """Execute an SQL command (or commands). |
| 327 | |
| 328 | This method can execute many SQL commands at once, when no arguments |
| 329 | are provided. |
| 330 | |
| 331 | Example: |
| 332 | |
| 333 | .. code-block:: pycon |
| 334 | |
| 335 | >>> await con.execute(''' |
| 336 | ... CREATE TABLE mytab (a int); |
| 337 | ... INSERT INTO mytab (a) VALUES (100), (200), (300); |
| 338 | ... ''') |
| 339 | INSERT 0 3 |
| 340 | |
| 341 | >>> await con.execute(''' |
| 342 | ... INSERT INTO mytab (a) VALUES ($1), ($2) |
| 343 | ... ''', 10, 20) |
| 344 | INSERT 0 2 |
| 345 | |
| 346 | :param args: Query arguments. |
| 347 | :param float timeout: Optional timeout value in seconds. |
| 348 | :return str: Status of the last SQL command. |
| 349 | |
| 350 | When query arguments are provided, the command is executed as a |
| 351 | prepared statement and is eligible for the connection's LRU |
| 352 | statement cache. |
| 353 | |
| 354 | The status string for an ``INSERT`` has the form |
| 355 | ``INSERT <oid> <count>``. A result such as ``INSERT 0 0`` is a |
| 356 | legitimate outcome for queries that insert zero rows (for example, |
| 357 | ``INSERT ... SELECT ... WHERE false``), and does not indicate a |
| 358 | failure. To confirm that a single row was inserted, check the |
| 359 | status string: |
| 360 | |
| 361 | .. code-block:: pycon |
| 362 | |
| 363 | >>> result = await con.execute( |
| 364 | ... 'INSERT INTO mytab (a) VALUES ($1)', 1) |
| 365 | >>> assert result == 'INSERT 0 1' |
| 366 | |
| 367 | .. versionchanged:: 0.5.4 |
| 368 | Made it possible to pass query arguments. |
| 369 | """ |
| 370 | self._check_open() |
| 371 | |
| 372 | if not args: |
| 373 | if self._query_loggers: |
| 374 | with self._time_and_log(query, args, timeout): |
| 375 | result = await self._protocol.query(query, timeout) |
| 376 | else: |
| 377 | result = await self._protocol.query(query, timeout) |
no test coverage detected