1
`psycopg2.extras` -- Miscellaneous goodies for Psycopg 2
2
=============================================================
4
.. sectionauthor:: Daniele Varrazzo <daniele.varrazzo@gmail.com>
6
.. module:: psycopg2.extras
10
import psycopg2.extras
11
from psycopg2.extras import Inet
15
This module is a generic place used to hold little helper functions and
16
classes until a better place in the distribution is found.
20
pair: Cursor; Dictionary
24
Dictionary-like cursor
25
----------------------
27
The dict cursors allow to access to the retrieved records using an iterface
28
similar to the Python dictionaries instead of the tuples. You can use it
29
either passing `DictConnection` as `connection_factory` argument
30
to the `~psycopg2.connect()` function or passing `DictCursor` as
31
the `!cursor_factory` argument to the `~connection.cursor()` method
32
of a regular `connection`.
34
>>> dict_cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
35
>>> dict_cur.execute("INSERT INTO test (num, data) VALUES(%s, %s)",
37
>>> dict_cur.execute("SELECT * FROM test")
38
>>> rec = dict_cur.fetchone()
46
The records still support indexing as the original tuple:
52
.. autoclass:: DictCursor
54
.. autoclass:: DictConnection
56
.. autoclass:: DictRow
59
Real dictionary cursor
60
^^^^^^^^^^^^^^^^^^^^^^
62
.. autoclass:: RealDictCursor
64
.. autoclass:: RealDictConnection
66
.. autoclass:: RealDictRow
76
.. autoclass:: LoggingConnection
77
:members: initialize,filter
79
.. autoclass:: LoggingCursor
82
.. autoclass:: MinTimeLoggingConnection
83
:members: initialize,filter
85
.. autoclass:: MinTimeLoggingCursor
90
pair: UUID; Data types
95
.. versionadded:: 2.0.9
96
.. versionchanged:: 2.0.13 added UUID array support.
100
>>> psycopg2.extras.register_uuid()
101
<psycopg2._psycopg.type object at 0x...>
103
>>> # Python UUID can be used in SQL queries
105
>>> my_uuid = uuid.UUID('{12345678-1234-5678-1234-567812345678}')
106
>>> psycopg2.extensions.adapt(my_uuid).getquoted()
107
"'12345678-1234-5678-1234-567812345678'::uuid"
109
>>> # PostgreSQL UUID are transformed into Python UUID objects.
110
>>> cur.execute("SELECT 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'::uuid")
111
>>> cur.fetchone()[0]
112
UUID('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11')
115
.. autofunction:: register_uuid
117
.. autoclass:: UUID_adapter
122
pair: INET; Data types
124
:sql:`inet` data type
125
----------------------
127
.. versionadded:: 2.0.9
131
>>> psycopg2.extras.register_inet()
132
<psycopg2._psycopg.type object at 0x...>
134
>>> cur.mogrify("SELECT %s", (Inet('127.0.0.1/32'),))
135
"SELECT E'127.0.0.1/32'::inet"
137
>>> cur.execute("SELECT '192.168.0.1/24'::inet")
138
>>> cur.fetchone()[0].addr
142
.. autofunction:: register_inet()
149
single: Time zones; Fractional
151
Fractional time zones
152
---------------------
154
.. autofunction:: register_tstz_w_secs
156
.. versionadded:: 2.0.9
159
pair: Example; Coroutine;
164
.. autofunction:: wait_select(conn)