Ticket #9206: 9206-r9084-raw-sql-docs.diff
File 9206-r9084-raw-sql-docs.diff, 4.9 KB (added by , 16 years ago) |
---|
-
docs/topics/db/models.txt
725 725 -------------------- 726 726 727 727 Another common pattern is writing custom SQL statements in model methods and 728 module-level methods. The object :class:`django.db.connection 729 <django.db.backends.DatabaseWrapper>` represents the current database 730 connection. To use it, call :meth:`connection.cursor() 731 <django.db.backends.DatabaseWrapper.cursor>` to get a cursor object. Then, call 732 ``cursor.execute(sql, [params])`` to execute the SQL and 733 :meth:`cursor.fetchone() <django.db.backends.CursorWrapper.fetchone>` or 734 :meth:`cursor.fetchall() <django.db.backends.CursorWrapper.fetchall>` to return 735 the resulting rows. For example:: 728 module-level methods. See :ref:`raw SQL <topic-db-sql>`. 736 729 737 def my_custom_sql(self):738 from django.db import connection739 cursor = connection.cursor()740 cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])741 row = cursor.fetchone()742 return row743 744 :class:`connection <django.db.backends.DatabaseWrapper>` and :class:`cursor745 <django.db.backends.CursorWrapper>` mostly implement the standard Python746 DB-API -- see :pep:`249` -- with the addition of Django's :ref:`transaction747 handling <topics-db-transactions>`. If you're not familiar with the Python748 DB-API, note that the SQL statement in :meth:`cursor.execute()749 <django.db.backends.CursorWrapper.execute>` uses placeholders, ``"%s"``, rather750 than adding parameters directly within the SQL. If you use this technique, the751 underlying database library will automatically add quotes and escaping to your752 parameter(s) as necessary. (Also note that Django expects the ``"%s"``753 placeholder, *not* the ``"?"`` placeholder, which is used by the SQLite Python754 bindings. This is for the sake of consistency and sanity.)755 756 A final note: If all you want to do is a custom ``WHERE`` clause, you can use757 the :meth:`~QuerySet.extra` lookup method, which lets you add custom SQL to a758 query.759 760 730 .. _model-inheritance: 761 731 762 732 Model inheritance -
docs/topics/db/sql.txt
5 5 6 6 Feel free to write custom SQL statements in custom model methods and 7 7 module-level methods. The object ``django.db.connection`` represents the 8 current database connection. To use it, call ``connection.cursor()`` to get a 8 current database connection, and ``django.db.transaction`` represents the 9 current database transaction. To use it, call ``connection.cursor()`` to get a 9 10 cursor object. Then, call ``cursor.execute(sql, [params])`` to execute the SQL 10 11 and ``cursor.fetchone()`` or ``cursor.fetchall()`` to return the resulting 11 rows. Example:: 12 rows, followed by ``transaction.commit_unless_managed()`` to commit the 13 change. Example:: 12 14 13 15 def my_custom_sql(self): 14 from django.db import connection 16 from django.db import connection, transaction 15 17 cursor = connection.cursor() 16 18 cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz]) 17 19 row = cursor.fetchone() 20 cursor.execute("UPDATE bar SET foo = 1 WHERE baz = %s", [self.baz]) 21 transaction.commit_unless_managed() 18 22 return row 19 23 20 24 ``connection`` and ``cursor`` mostly implement the standard `Python DB-API`_ … … 31 35 use the ``where``, ``tables`` and ``params`` arguments to the standard lookup 32 36 API. 33 37 38 Transaction handling 39 -------------------- 40 41 Django :ref:`transaction handling <topic-db-transactions>` offers a choice 42 of different behaviors, if you're using a database that supports 43 transactions. 44 45 Raw SQL users should note that all their commands are actually always run 46 inside a transaction, even when using Django in auto-commit mode. As per the 47 standard `Python DB-API`, all database connections come wrapped inside a 48 transaction. In the default Django auto-commit mode, it simulates 49 auto-commit by regularly committing this underlying transaction, e.g. in 50 ``model.save()``. 51 52 As a result, raw SQL users should call 53 ``transaction.commit_unless_managed()`` after ``cursor.execute()``, just as 54 ``model.save()`` does internally. 55 56 Raw SQL users with PostgreSQL should note that once a PostgreSQL raises an 57 exception, all subsequent SQL in the same transaction fails with the error 58 "current transaction is aborted, queries ignored until end of transaction 59 block". As a result, raw SQL users with PostgreSQL should additionally wrap 60 commands that may throw IntegrityError inside savepoints. Example:: 61 62 try: 63 sid = transaction.savepoint() 64 cursor.execute(XXX) 65 transaction.savepoint_commit(sid) 66 except IntegrityError: 67 transaction.savepoint_rollback(sid) 68 raise 69 34 70 .. _Python DB-API: http://www.python.org/peps/pep-0249.html 35 71