mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Fixed #17671 - Cursors are now context managers.
This commit is contained in:
parent
04a2a6b0f9
commit
99c87f1410
6 changed files with 85 additions and 1 deletions
|
@ -297,3 +297,30 @@ database library will automatically escape your parameters as necessary.
|
|||
Also note that Django expects the ``"%s"`` placeholder, *not* the ``"?"``
|
||||
placeholder, which is used by the SQLite Python bindings. This is for the sake
|
||||
of consistency and sanity.
|
||||
|
||||
.. versionchanged:: 1.7
|
||||
|
||||
:pep:`249` does not state whether a cursor should be usable as a context
|
||||
manager. Prior to Python 2.7, a cursor was usable as a context manager due
|
||||
an unexpected behavior in magic method lookups (`Python ticket #9220`_).
|
||||
Django 1.7 explicitly added support to allow using a cursor as context
|
||||
manager.
|
||||
|
||||
.. _`Python ticket #9220`: http://bugs.python.org/issue9220
|
||||
|
||||
Using a cursor as a context manager:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
with connection.cursor() as c:
|
||||
c.execute(...)
|
||||
|
||||
is equivalent to:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
c = connection.cursor()
|
||||
try:
|
||||
c.execute(...)
|
||||
finally:
|
||||
c.close()
|
Loading…
Add table
Add a link
Reference in a new issue