2010-02-09 07:58:28 +03:00
|
|
|
The ``cursor`` class
|
|
|
|
====================
|
|
|
|
|
|
|
|
.. sectionauthor:: Daniele Varrazzo <daniele.varrazzo@gmail.com>
|
|
|
|
|
2010-02-13 19:06:39 +03:00
|
|
|
.. testsetup:: *
|
|
|
|
|
|
|
|
from StringIO import StringIO
|
|
|
|
import sys
|
|
|
|
|
|
|
|
create_test_table()
|
|
|
|
|
|
|
|
# initial data
|
|
|
|
cur.executemany("INSERT INTO test (num, data) VALUES (%s, %s)",
|
|
|
|
[(100, "abc'def"), (None, 'dada'), (42, 'bar')])
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. class:: cursor
|
|
|
|
|
|
|
|
Allows Python code to execute PostgreSQL command in a database session.
|
2010-02-11 06:15:14 +03:00
|
|
|
Cursors are created by the :meth:`connection.cursor` method: they are
|
|
|
|
bound to the connection for the entire lifetime and all the commands are
|
|
|
|
executed in the context of the database session wrapped by the connection.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
Cursors created from the same connection are not isolated, i.e., any
|
|
|
|
changes done to the database by a cursor are immediately visible by the
|
|
|
|
other cursors. Cursors created from different connections can or can not
|
2010-02-11 06:15:14 +03:00
|
|
|
be isolated, depending on the connections' :ref:`isolation level
|
|
|
|
<transactions-control>`. See also :meth:`~connection.rollback` and
|
|
|
|
:meth:`~connection.commit` methods.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
Cursors are *not* thread safe: a multithread application can create
|
2010-02-18 04:08:44 +03:00
|
|
|
many cursors from the same connection and should use each cursor from
|
2010-02-09 07:58:28 +03:00
|
|
|
a single thread. See :ref:`thread-safety` for details.
|
|
|
|
|
|
|
|
|
|
|
|
.. attribute:: description
|
|
|
|
|
|
|
|
This read-only attribute is a sequence of 7-item sequences.
|
|
|
|
|
|
|
|
Each of these sequences contains information describing one result
|
|
|
|
column:
|
|
|
|
|
|
|
|
- ``name``
|
|
|
|
- ``type_code``
|
|
|
|
- ``display_size``
|
|
|
|
- ``internal_size``
|
|
|
|
- ``precision``
|
|
|
|
- ``scale``
|
|
|
|
- ``null_ok``
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
The first two items (``name`` and ``type_code``) are always specified,
|
|
|
|
the other five are optional and are set to ``None`` if no meaningful
|
2010-02-09 07:58:28 +03:00
|
|
|
values can be provided.
|
|
|
|
|
|
|
|
This attribute will be ``None`` for operations that do not return rows
|
|
|
|
or if the cursor has not had an operation invoked via the
|
|
|
|
|execute*|_ methods yet.
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
The ``type_code`` can be interpreted by comparing it to the Type
|
|
|
|
Objects specified in the section :ref:`type-objects-and-constructors`.
|
|
|
|
It is also used to register typecasters to convert PostgreSQL types to
|
|
|
|
Python objects: see :ref:`type-casting-from-sql-to-python`.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. method:: close()
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
Close the cursor now (rather than whenever :meth:`!__del__` is
|
|
|
|
called). The cursor will be unusable from this point forward; an
|
|
|
|
:exc:`~psycopg2.InterfaceError` will be raised if any operation is
|
|
|
|
attempted with the cursor.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
.. attribute:: closed
|
|
|
|
|
|
|
|
Read-only boolean attribute: specifies if the cursor is closed
|
|
|
|
(``True``) or not (``False``).
|
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
.. extension::
|
|
|
|
|
|
|
|
The :attr:`closed` attribute is a Psycopg extension to the
|
|
|
|
|DBAPI|.
|
|
|
|
|
2010-02-13 08:49:34 +03:00
|
|
|
.. versionadded:: 2.0.7
|
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. attribute:: connection
|
|
|
|
|
|
|
|
Read-only attribute returning a reference to the :class:`connection`
|
|
|
|
object on which the cursor was created.
|
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. attribute:: name
|
|
|
|
|
|
|
|
Read-only attribute containing the name of the cursor if it was
|
|
|
|
creates as named cursor by :meth:`connection.cursor`, or ``None`` if
|
|
|
|
it is a client side cursor. See :ref:`server-side-cursors`.
|
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
.. extension::
|
|
|
|
|
|
|
|
The :attr:`name` attribute is a Psycopg extension to the |DBAPI|.
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
.. |execute*| replace:: :meth:`execute*`
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
.. _execute*:
|
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
.. rubric:: Commands execution methods
|
|
|
|
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. method:: execute(operation [, parameters] [, async])
|
|
|
|
|
|
|
|
Prepare and execute a database operation (query or command).
|
|
|
|
|
|
|
|
Parameters may be provided as sequence or mapping and will be bound to
|
|
|
|
variables in the operation. Variables are specified either with
|
2010-02-11 06:15:14 +03:00
|
|
|
positional (``%s``) or named (:samp:`%({name})s`) placeholders. See
|
2010-02-09 07:58:28 +03:00
|
|
|
:ref:`query-parameters`.
|
|
|
|
|
|
|
|
The method returns `None`. If a query was executed, the returned
|
|
|
|
values can be retrieved using |fetch*|_ methods.
|
|
|
|
|
2010-02-18 04:08:44 +03:00
|
|
|
If `async` is ``True``, query execution will be asynchronous:
|
2010-02-11 06:15:14 +03:00
|
|
|
the function returns immediately while the query is executed by the
|
|
|
|
backend. Use the :meth:`~cursor.isready` method to see if the data is
|
2010-02-09 07:58:28 +03:00
|
|
|
ready for return via |fetch*|_ methods. See
|
|
|
|
:ref:`asynchronous-queries`.
|
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
.. extension::
|
|
|
|
|
2010-02-18 04:08:44 +03:00
|
|
|
The `async` parameter is a Psycopg extension to the |DBAPI|.
|
2010-02-10 02:09:48 +03:00
|
|
|
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
.. method:: mogrify(operation [, parameters])
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
Return a query string after arguments binding. The string returned is
|
|
|
|
exactly the one that would be sent to the database running the
|
2010-02-13 19:06:39 +03:00
|
|
|
:meth:`~cursor.execute` method or similar.
|
2010-02-11 06:15:14 +03:00
|
|
|
|
|
|
|
>>> cur.mogrify("INSERT INTO test (num, data) VALUES (%s, %s)", (42, 'bar'))
|
|
|
|
"INSERT INTO test (num, data) VALUES (42, E'bar')"
|
2010-02-10 02:09:48 +03:00
|
|
|
|
|
|
|
.. extension::
|
|
|
|
|
|
|
|
The :meth:`mogrify` method is a Psycopg extension to the |DBAPI|.
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
.. method:: executemany(operation, seq_of_parameters)
|
|
|
|
|
|
|
|
Prepare a database operation (query or command) and then execute it
|
2010-02-11 06:15:14 +03:00
|
|
|
against all parameter tuples or mappings found in the sequence
|
2010-02-18 04:08:44 +03:00
|
|
|
`seq_of_parameters`.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
The function is mostly useful for commands that update the database:
|
|
|
|
any result set returned by the query is discarded.
|
|
|
|
|
|
|
|
Parameters are bounded to the query using the same rules described in
|
2010-02-11 06:15:14 +03:00
|
|
|
the :meth:`~cursor.execute` method.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. method:: callproc(procname [, parameters] [, async])
|
|
|
|
|
|
|
|
Call a stored database procedure with the given name. The sequence of
|
|
|
|
parameters must contain one entry for each argument that the procedure
|
|
|
|
expects. The result of the call is returned as modified copy of the
|
|
|
|
input sequence. Input parameters are left untouched, output and
|
|
|
|
input/output parameters replaced with possibly new values.
|
|
|
|
|
|
|
|
The procedure may also provide a result set as output. This must then
|
|
|
|
be made available through the standard |fetch*|_ methods.
|
|
|
|
|
2010-02-18 04:08:44 +03:00
|
|
|
If `async` is ``True``, procedure execution will be asynchronous:
|
2010-02-09 07:58:28 +03:00
|
|
|
the function returns immediately while the procedure is executed by
|
2010-02-11 06:15:14 +03:00
|
|
|
the backend. Use the :meth:`~cursor.isready` method to see if the
|
|
|
|
data is ready for return via |fetch*|_ methods. See
|
2010-02-09 07:58:28 +03:00
|
|
|
:ref:`asynchronous-queries`.
|
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
.. extension::
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-18 04:08:44 +03:00
|
|
|
The `async` parameter is a Psycopg extension to the |DBAPI|.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
.. method:: setinputsizes(sizes)
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
This method is exposed in compliance with the |DBAPI|. It currently
|
2010-02-10 02:09:48 +03:00
|
|
|
does nothing but it is safe to call it.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
.. |fetch*| replace:: :meth:`!fetch*`
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
.. _fetch*:
|
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
.. rubric:: Results retrieval methods
|
|
|
|
|
|
|
|
|
2010-02-09 16:33:31 +03:00
|
|
|
The following methods are used to read data from the database after an
|
2010-02-11 06:15:14 +03:00
|
|
|
:meth:`~cursor.execute` call.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
:class:`cursor` objects are iterable, so, instead of calling
|
2010-02-11 06:15:14 +03:00
|
|
|
explicitly :meth:`~cursor.fetchone` in a loop, the object itself can
|
2010-02-13 19:06:39 +03:00
|
|
|
be used:
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
>>> cur.execute("SELECT * FROM test;")
|
|
|
|
>>> for record in cur:
|
|
|
|
... print record
|
|
|
|
...
|
|
|
|
(1, 100, "abc'def")
|
|
|
|
(2, None, 'dada')
|
2010-02-13 19:06:39 +03:00
|
|
|
(3, 42, 'bar')
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. method:: fetchone()
|
|
|
|
|
|
|
|
Fetch the next row of a query result set, returning a single tuple,
|
2010-02-13 19:06:39 +03:00
|
|
|
or ``None`` when no more data is available:
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-13 19:06:39 +03:00
|
|
|
>>> cur.execute("SELECT * FROM test WHERE id = %s", (3,))
|
2010-02-09 07:58:28 +03:00
|
|
|
>>> cur.fetchone()
|
2010-02-13 19:06:39 +03:00
|
|
|
(3, 42, 'bar')
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
A :exc:`~psycopg2.ProgrammingError` is raised if the previous call
|
2010-02-09 07:58:28 +03:00
|
|
|
to |execute*|_ did not produce any result set or no call was issued
|
|
|
|
yet.
|
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. method:: fetchmany([size=cursor.arraysize])
|
|
|
|
|
|
|
|
Fetch the next set of rows of a query result, returning a list of
|
|
|
|
tuples. An empty list is returned when no more rows are available.
|
|
|
|
|
|
|
|
The number of rows to fetch per call is specified by the parameter.
|
2010-02-11 06:15:14 +03:00
|
|
|
If it is not given, the cursor's :attr:`~cursor.arraysize` determines
|
|
|
|
the number of rows to be fetched. The method should try to fetch as
|
|
|
|
many rows as indicated by the size parameter. If this is not possible
|
|
|
|
due to the specified number of rows not being available, fewer rows
|
2010-02-13 19:06:39 +03:00
|
|
|
may be returned:
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
>>> cur.execute("SELECT * FROM test;")
|
|
|
|
>>> cur.fetchmany(2)
|
|
|
|
[(1, 100, "abc'def"), (2, None, 'dada')]
|
|
|
|
>>> cur.fetchmany(2)
|
2010-02-13 19:06:39 +03:00
|
|
|
[(3, 42, 'bar')]
|
2010-02-09 07:58:28 +03:00
|
|
|
>>> cur.fetchmany(2)
|
|
|
|
[]
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
A :exc:`~psycopg2.ProgrammingError` is raised if the previous call to
|
|
|
|
|execute*|_ did not produce any result set or no call was issued yet.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
Note there are performance considerations involved with the size
|
|
|
|
parameter. For optimal performance, it is usually best to use the
|
2010-02-11 06:15:14 +03:00
|
|
|
:attr:`~cursor.arraysize` attribute. If the size parameter is used,
|
|
|
|
then it is best for it to retain the same value from one
|
|
|
|
:meth:`fetchmany` call to the next.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. method:: fetchall()
|
|
|
|
|
|
|
|
Fetch all (remaining) rows of a query result, returning them as a list
|
2010-02-11 06:15:14 +03:00
|
|
|
of tuples. An empty list is returned if there is no more record to
|
|
|
|
fetch.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
>>> cur.execute("SELECT * FROM test;")
|
|
|
|
>>> cur.fetchall()
|
2010-02-13 19:06:39 +03:00
|
|
|
[(1, 100, "abc'def"), (2, None, 'dada'), (3, 42, 'bar')]
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
A :exc:`~psycopg2.ProgrammingError` is raised if the previous call to
|
|
|
|
|execute*|_ did not produce any result set or no call was issued yet.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
.. method:: scroll(value [, mode='relative'])
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
Scroll the cursor in the result set to a new position according
|
|
|
|
to mode.
|
|
|
|
|
2010-02-18 04:08:44 +03:00
|
|
|
If `mode` is ``relative`` (default), value is taken as offset to
|
2010-02-09 07:58:28 +03:00
|
|
|
the current position in the result set, if set to ``absolute``,
|
|
|
|
value states an absolute target position.
|
|
|
|
|
|
|
|
If the scroll operation would leave the result set, a
|
2010-02-11 06:15:14 +03:00
|
|
|
:exc:`~psycopg2.ProgrammingError` is raised and the cursor position is
|
|
|
|
not changed.
|
|
|
|
|
|
|
|
The method can be used both for client-side cursors and
|
|
|
|
:ref:`server-side cursors <server-side-cursors>`.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
.. note::
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
According to the |DBAPI|_, the exception raised for a cursor out
|
2010-02-13 00:52:25 +03:00
|
|
|
of bound should have been :exc:`!IndexError`. The best option is
|
2010-02-15 06:08:51 +03:00
|
|
|
probably to catch both exceptions in your code::
|
2010-02-13 00:52:25 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
cur.scroll(1000 * 1000)
|
|
|
|
except (ProgrammingError, IndexError), exc:
|
|
|
|
deal_with_it(exc)
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. attribute:: arraysize
|
|
|
|
|
|
|
|
This read/write attribute specifies the number of rows to fetch at a
|
2010-02-11 06:15:14 +03:00
|
|
|
time with :meth:`~cursor.fetchmany`. It defaults to 1 meaning to fetch
|
|
|
|
a single row at a time.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. attribute:: rowcount
|
|
|
|
|
|
|
|
This read-only attribute specifies the number of rows that the last
|
2010-02-11 06:15:14 +03:00
|
|
|
|execute*|_ produced (for :abbr:`DQL (Data Query Language)` statements
|
|
|
|
like :sql:`SELECT`) or affected (for
|
|
|
|
:abbr:`DML (Data Manipulation Language)` statements like :sql:`UPDATE`
|
|
|
|
or :sql:`INSERT`).
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
The attribute is -1 in case no |execute*| has been performed on
|
2010-02-09 16:33:31 +03:00
|
|
|
the cursor or the row count of the last operation if it can't be
|
2010-02-09 07:58:28 +03:00
|
|
|
determined by the interface.
|
|
|
|
|
|
|
|
.. note::
|
2010-02-10 00:31:40 +03:00
|
|
|
The |DBAPI|_ interface reserves to redefine the latter case to
|
2010-02-09 07:58:28 +03:00
|
|
|
have the object return ``None`` instead of -1 in future versions
|
|
|
|
of the specification.
|
2010-02-10 02:09:48 +03:00
|
|
|
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. attribute:: rownumber
|
|
|
|
|
|
|
|
This read-only attribute provides the current 0-based index of the
|
|
|
|
cursor in the result set or ``None`` if the index cannot be
|
|
|
|
determined.
|
|
|
|
|
|
|
|
The index can be seen as index of the cursor in a sequence (the result
|
|
|
|
set). The next fetch operation will fetch the row indexed by
|
|
|
|
:attr:`rownumber` in that sequence.
|
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. index:: oid
|
|
|
|
|
|
|
|
.. attribute:: lastrowid
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
This read-only attribute provides the OID of the last row inserted
|
|
|
|
by the cursor. If the table wasn't created with OID support or the
|
2010-02-09 07:58:28 +03:00
|
|
|
last operation is not a single record insert, the attribute is set to
|
|
|
|
``None``.
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
PostgreSQL currently advices to not create OIDs on the tables and the
|
2010-02-09 07:58:28 +03:00
|
|
|
default for |CREATE-TABLE|__ is to not support them. The
|
|
|
|
|INSERT-RETURNING|__ syntax available from PostgreSQL 8.3 allows more
|
2010-02-18 04:08:44 +03:00
|
|
|
flexibility.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
.. |CREATE-TABLE| replace:: :sql:`CREATE TABLE`
|
2010-02-09 07:58:28 +03:00
|
|
|
.. __: http://www.postgresql.org/docs/8.4/static/sql-createtable.html
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
.. |INSERT-RETURNING| replace:: :sql:`INSERT ... RETURNING`
|
2010-02-09 07:58:28 +03:00
|
|
|
.. __: http://www.postgresql.org/docs/8.4/static/sql-insert.html
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: nextset()
|
|
|
|
|
|
|
|
This method is not supported (PostgreSQL does not have multiple data
|
2010-02-11 06:15:14 +03:00
|
|
|
sets) and will raise a :exc:`~psycopg2.NotSupportedError` exception.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
|
|
|
|
.. method:: setoutputsize(size [, column])
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
This method is exposed in compliance with the |DBAPI|. It currently
|
2010-02-10 02:09:48 +03:00
|
|
|
does nothing but it is safe to call it.
|
|
|
|
|
|
|
|
|
|
|
|
.. attribute:: query
|
|
|
|
|
|
|
|
Read-only attribute containing the body of the last query sent to the
|
|
|
|
backend (including bound arguments). ``None`` if no query has been
|
2010-02-13 19:06:39 +03:00
|
|
|
executed yet:
|
2010-02-10 02:09:48 +03:00
|
|
|
|
|
|
|
>>> cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (42, 'bar'))
|
|
|
|
>>> cur.query
|
|
|
|
"INSERT INTO test (num, data) VALUES (42, E'bar')"
|
|
|
|
|
|
|
|
.. extension::
|
|
|
|
|
|
|
|
The :attr:`query` attribute is a Psycopg extension to the |DBAPI|.
|
|
|
|
|
|
|
|
|
|
|
|
.. attribute:: statusmessage
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
Read-only attribute containing the message returned by the last
|
2010-02-13 19:06:39 +03:00
|
|
|
command:
|
2010-02-10 02:09:48 +03:00
|
|
|
|
|
|
|
>>> cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (42, 'bar'))
|
|
|
|
>>> cur.statusmessage
|
|
|
|
'INSERT 0 1'
|
|
|
|
|
|
|
|
.. extension::
|
|
|
|
|
|
|
|
The :attr:`statusmessage` attribute is a Psycopg extension to the
|
|
|
|
|DBAPI|.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
.. method:: isready()
|
|
|
|
|
|
|
|
Return ``False`` if the backend is still processing an asynchronous
|
|
|
|
query or ``True`` if data is ready to be fetched by one of the
|
|
|
|
|fetch*|_ methods. See :ref:`asynchronous-queries`.
|
|
|
|
|
|
|
|
.. extension::
|
|
|
|
|
|
|
|
The :meth:`isready` method is a Psycopg extension to the |DBAPI|.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: fileno()
|
|
|
|
|
|
|
|
Return the file descriptor associated with the current connection and
|
|
|
|
make possible to use a cursor in a context where a file object would
|
2010-02-11 06:15:14 +03:00
|
|
|
be expected (like in a :func:`select` call). See
|
2010-02-10 02:09:48 +03:00
|
|
|
:ref:`asynchronous-queries`.
|
|
|
|
|
|
|
|
.. extension::
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
The :meth:`fileno` method is a Psycopg extension to the |DBAPI|.
|
2010-02-10 02:09:48 +03:00
|
|
|
|
|
|
|
|
2010-02-14 22:42:15 +03:00
|
|
|
.. attribute:: tzinfo_factory
|
|
|
|
|
|
|
|
The time zone factory used to handle data types such as
|
|
|
|
:sql:`TIMESTAMP WITH TIME ZONE`. It should be a |tzinfo|_ object.
|
|
|
|
See also the :mod:`psycopg2.tz` module.
|
|
|
|
|
|
|
|
.. |tzinfo| replace:: :class:`!tzinfo`
|
|
|
|
.. _tzinfo: http://docs.python.org/library/datetime.html#tzinfo-objects
|
|
|
|
|
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
|
|
|
|
.. rubric:: COPY-related methods
|
|
|
|
|
|
|
|
.. extension::
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
The :sql:`COPY` command is a PostgreSQL extension to the SQL standard.
|
|
|
|
As such, its support is a Psycopg extension to the |DBAPI|.
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-09 19:30:52 +03:00
|
|
|
.. method:: copy_from(file, table, sep='\\t', null='\\N', columns=None)
|
|
|
|
|
2010-02-18 04:08:44 +03:00
|
|
|
Read data *from* the file-like object `file` appending them to
|
|
|
|
the table named `table`. `file` must have both
|
2010-02-11 06:15:14 +03:00
|
|
|
:meth:`!read` and :meth:`!readline` method. See :ref:`copy` for an
|
|
|
|
overview.
|
2010-02-10 03:10:51 +03:00
|
|
|
|
2010-02-18 04:08:44 +03:00
|
|
|
The optional argument `sep` is the columns separator and
|
|
|
|
`null` represents :sql:`NULL` values in the file.
|
2010-02-10 03:10:51 +03:00
|
|
|
|
2010-02-18 04:08:44 +03:00
|
|
|
The `columns` argument is a sequence containing the name of the
|
2010-02-10 03:10:51 +03:00
|
|
|
fields where the read data will be entered. Its length and column
|
|
|
|
type should match the content of the read file. If not specifies, it
|
2010-02-13 19:06:39 +03:00
|
|
|
is assumed that the entire table matches the file structure.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-09 19:30:52 +03:00
|
|
|
>>> f = StringIO("42\tfoo\n74\tbar\n")
|
|
|
|
>>> cur.copy_from(f, 'test', columns=('num', 'data'))
|
|
|
|
>>> cur.execute("select * from test where id > 5;")
|
|
|
|
>>> cur.fetchall()
|
2010-02-13 19:06:39 +03:00
|
|
|
[(6, 42, 'foo'), (7, 74, 'bar')]
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-09 19:30:52 +03:00
|
|
|
.. versionchanged:: 2.0.6
|
2010-02-18 04:08:44 +03:00
|
|
|
added the `columns` parameter.
|
2010-02-09 19:30:52 +03:00
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-09 19:30:52 +03:00
|
|
|
.. method:: copy_to(file, table, sep='\\t', null='\\N', columns=None)
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-18 04:08:44 +03:00
|
|
|
Write the content of the table named `table` *to* the file-like
|
|
|
|
object `file`. `file` must have a :meth:`!write` method.
|
2010-02-11 06:15:14 +03:00
|
|
|
See :ref:`copy` for an overview.
|
2010-02-09 19:30:52 +03:00
|
|
|
|
2010-02-18 04:08:44 +03:00
|
|
|
The optional argument `sep` is the columns separator and
|
|
|
|
`null` represents :sql:`NULL` values in the file.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-18 04:08:44 +03:00
|
|
|
The `columns` argument is a sequence of field names: if not
|
2010-02-13 19:06:39 +03:00
|
|
|
``None`` only the specified fields will be included in the dump.
|
2010-02-10 03:10:51 +03:00
|
|
|
|
|
|
|
>>> cur.copy_to(sys.stdout, 'test', sep="|")
|
|
|
|
1|100|abc'def
|
|
|
|
2|\N|dada
|
2010-02-13 19:06:39 +03:00
|
|
|
...
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-09 19:30:52 +03:00
|
|
|
.. versionchanged:: 2.0.6
|
2010-02-18 04:08:44 +03:00
|
|
|
added the `columns` parameter.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-09 19:30:52 +03:00
|
|
|
.. method:: copy_expert(sql, file [, size])
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
Submit a user-composed :sql:`COPY` statement. The method is useful to
|
2010-02-09 19:30:52 +03:00
|
|
|
handle all the parameters that PostgreSQL makes available (see
|
|
|
|
|COPY|__ command documentation).
|
|
|
|
|
2010-02-18 04:08:44 +03:00
|
|
|
`file` must be an open, readable file for :sql:`COPY FROM` or an
|
|
|
|
open, writeable file for :sql:`COPY TO`. The optional `size`
|
2010-02-11 06:15:14 +03:00
|
|
|
argument, when specified for a :sql:`COPY FROM` statement, will be
|
2010-02-18 04:08:44 +03:00
|
|
|
passed to `file`\ 's read method to control the read buffer
|
2010-02-13 19:06:39 +03:00
|
|
|
size.
|
2010-02-10 03:10:51 +03:00
|
|
|
|
2010-02-09 19:30:52 +03:00
|
|
|
>>> cur.copy_expert("COPY test TO STDOUT WITH CSV HEADER", sys.stdout)
|
|
|
|
id,num,data
|
|
|
|
1,100,abc'def
|
|
|
|
2,,dada
|
2010-02-13 19:06:39 +03:00
|
|
|
...
|
2010-02-09 19:30:52 +03:00
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
.. |COPY| replace:: :sql:`COPY`
|
2010-02-09 19:30:52 +03:00
|
|
|
.. __: http://www.postgresql.org/docs/8.4/static/sql-copy.html
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-09 19:30:52 +03:00
|
|
|
.. versionadded:: 2.0.6
|
2010-02-09 07:58:28 +03:00
|
|
|
|