|
39 | 39 | Bind Parameters
|
40 | 40 | +++++++++++++++
|
41 | 41 |
|
42 |
| -In case you're not familiar with bind parameters in DB-API 2.0, the basic idea |
43 |
| -is that you put ``%(foo)s`` in your SQL strings, and then pass in a second |
| 42 | +In case you're not familiar with bind parameters in `DB-API 2.0`_, the basic |
| 43 | +idea is that you put ``%(foo)s`` in your SQL strings, and then pass in a second |
44 | 44 | argument, a :py:class:`dict`, containing parameters that :py:mod:`psycopg2` (as
|
45 | 45 | an implementation of DB-API 2.0) will bind to the query in a way that is safe
|
46 |
| -against SQL injection. (This is inspired by old-style Python string formatting, |
47 |
| -but it is not the same.) |
| 46 | +against `SQL injection`_. (This is inspired by old-style Python string |
| 47 | +formatting, but it is not the same.) |
48 | 48 |
|
49 | 49 | >>> db.one("SELECT * FROM foo WHERE bar=%(bar)s", {"bar": "baz"})
|
50 | 50 | {'bar': 'baz'}
|
|
57 | 57 | Context Managers
|
58 | 58 | ++++++++++++++++
|
59 | 59 |
|
60 |
| -Eighty percent of your database usage should be covered by the simple API |
61 |
| -above. For the other 20%, :py:mod:`postgres` provides context managers for |
62 |
| -working at increasingly lower levels of abstraction. The lowest level of |
63 |
| -abstraction in :py:mod:`postgres` is a :py:mod:`psycopg2` connection pool that |
64 |
| -we configure and manage for you. Everything in :py:mod:`postgres`, both the |
65 |
| -simple API and the context managers, uses this connection pool. |
66 |
| -
|
67 |
| -Here's how to work directly with a `psycogpg2 cursor |
68 |
| -<http://initd.org/psycopg/docs/cursor.html>`_ while still taking advantage of |
| 60 | +Eighty percent of your database usage should be covered by the simple |
| 61 | +:py:meth:`~postgres.Postgres.run`, :py:meth:`~postgres.Postgres.one`, |
| 62 | +:py:meth:`~postgres.Postgres.rows` API introduced above. For the other 20%, |
| 63 | +:py:mod:`postgres` provides context managers for working at increasingly lower |
| 64 | +levels of abstraction. The lowest level of abstraction in :py:mod:`postgres` is |
| 65 | +a :py:mod:`psycopg2` connection pool that we configure and manage for you. |
| 66 | +Everything in :py:mod:`postgres`, both the simple API and the context managers, |
| 67 | +uses this connection pool. |
| 68 | +
|
| 69 | +Here's how to work directly with a :py:mod:`psycogpg2.cursor` (`docs |
| 70 | +<http://initd.org/psycopg/docs/cursor.html>`_) while still taking advantage of |
69 | 71 | connection pooling:
|
70 | 72 |
|
71 | 73 | >>> with db.get_cursor() as cursor:
|
|
124 | 126 | .. _psycopg2: http://initd.org/psycopg/
|
125 | 127 | .. _GitHub: https://github.com/gittip/postgres
|
126 | 128 | .. _PyPI: https://pypi.python.org/pypi/postgres
|
| 129 | +.. _DB-API 2.0: http://www.python.org/dev/peps/pep-0249/ |
| 130 | +.. _SQL injection: http://en.wikipedia.org/wiki/SQL_injection |
127 | 131 |
|
128 | 132 | """
|
129 | 133 | from __future__ import unicode_literals
|
|
0 commit comments