Ticket #9206: 9206-r10628-raw-sql-docs.diff
File 9206-r10628-raw-sql-docs.diff, 5.9 KB (added by , 16 years ago) |
---|
-
docs/topics/db/models.txt
752 752 -------------------- 753 753 754 754 Another common pattern is writing custom SQL statements in model methods and 755 module-level methods. The object :class:`django.db.connection 756 <django.db.backends.DatabaseWrapper>` represents the current database 757 connection. To use it, call :meth:`connection.cursor() 758 <django.db.backends.DatabaseWrapper.cursor>` to get a cursor object. Then, call 759 ``cursor.execute(sql, [params])`` to execute the SQL and 760 :meth:`cursor.fetchone() <django.db.backends.CursorWrapper.fetchone>` or 761 :meth:`cursor.fetchall() <django.db.backends.CursorWrapper.fetchall>` to return 762 the resulting rows. For example:: 755 module-level methods. See :ref:`raw SQL <topic-db-sql>`. 763 756 764 def my_custom_sql(self):765 from django.db import connection766 cursor = connection.cursor()767 cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])768 row = cursor.fetchone()769 return row770 771 :class:`connection <django.db.backends.DatabaseWrapper>` and :class:`cursor772 <django.db.backends.CursorWrapper>` mostly implement the standard Python773 DB-API -- see :pep:`249` -- with the addition of Django's :ref:`transaction774 handling <topics-db-transactions>`. If you're not familiar with the Python775 DB-API, note that the SQL statement in :meth:`cursor.execute()776 <django.db.backends.CursorWrapper.execute>` uses placeholders, ``"%s"``, rather777 than adding parameters directly within the SQL. If you use this technique, the778 underlying database library will automatically add quotes and escaping to your779 parameter(s) as necessary. (Also note that Django expects the ``"%s"``780 placeholder, *not* the ``"?"`` placeholder, which is used by the SQLite Python781 bindings. This is for the sake of consistency and sanity.)782 783 A final note: If all you want to do is a custom ``WHERE`` clause, you can use784 the :meth:`~QuerySet.extra` lookup method, which lets you add custom SQL to a785 query.786 787 757 .. _model-inheritance: 788 758 789 759 Model inheritance -
docs/topics/db/transactions.txt
10 10 Django's default transaction behavior 11 11 ===================================== 12 12 13 Django's default behavior is to commit automatically when any built-in,14 data-altering model function is called. For example, if you call 15 ``model.save()`` or ``model.delete()``, the change will be committed 16 immediately.13 Django's default behavior is to run with an open transaction which it 14 commits automatically when any built-in, data-altering model function is 15 called. For example, if you call ``model.save()`` or ``model.delete()``, the 16 change will be committed immediately. 17 17 18 18 This is much like the auto-commit setting for most databases. As soon as you 19 19 perform an action that needs to write to the database, Django produces the … … 165 165 handle transactions as explained in this document. 166 166 167 167 .. _information on MySQL transactions: http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-transactions.html 168 169 Transactions and savepoints in PostgreSQL 170 ========================================= 171 172 If you're using PostgreSQL 8, then be aware that once PostgreSQL raises an 173 exception, all subsequent SQL in the same transaction fails with the error 174 "current transaction is aborted, queries ignored until end of transaction 175 block". Whilst simple use of save() is unlikely to raise an exception in 176 PostgreSQL, there are many more advanced usage patterns which might: e.g. 177 save with unique fields, save with force_insert/force_update, custom SQL. 178 179 In any of these cases, you can wrap the command which may throw 180 IntegrityError inside savepoints, which will then allow subsequent commands 181 to proceed. Example:: 182 183 try: 184 sid = transaction.savepoint() 185 x.save() 186 transaction.savepoint_commit(sid) 187 except IntegrityError: 188 transaction.savepoint_rollback(sid) 189 raise 190 191 Transactions and autocommit in PostgreSQL 192 ========================================= 193 194 By default, Django starts a transaction when a database connection is first 195 used and regularly commits, as above. The PostgreSQL backends normally 196 operate the same as any other Django backend in this respect, but can 197 additionally support database-level :ref:`autocommit mode <ref-databases>` 198 with no constantly open transaction. -
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. ``transaction.commit_unless_managed()`` is needed after 13 data-changing operations but not after pure selects, etc. 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`_