sql.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. ==========================
  2. Performing raw SQL queries
  3. ==========================
  4. .. currentmodule:: django.db.models
  5. When the :doc:`model query APIs </topics/db/queries>` don't go far enough, you
  6. can fall back to writing raw SQL. Django gives you two ways of performing raw
  7. SQL queries: you can use :meth:`Manager.raw()` to `perform raw queries and
  8. return model instances`__, or you can avoid the model layer entirely and
  9. `execute custom SQL directly`__.
  10. __ `performing raw queries`_
  11. __ `executing custom SQL directly`_
  12. .. _executing-raw-queries:
  13. Performing raw queries
  14. ======================
  15. The ``raw()`` manager method can be used to perform raw SQL queries that
  16. return model instances:
  17. .. method:: Manager.raw(raw_query, params=None, translations=None)
  18. This method takes a raw SQL query, executes it, and returns a
  19. ``django.db.models.query.RawQuerySet`` instance. This ``RawQuerySet`` instance
  20. can be iterated over just like a normal QuerySet to provide object instances.
  21. This is best illustrated with an example. Suppose you have the following model::
  22. class Person(models.Model):
  23. first_name = models.CharField(...)
  24. last_name = models.CharField(...)
  25. birth_date = models.DateField(...)
  26. You could then execute custom SQL like so::
  27. >>> for p in Person.objects.raw('SELECT * FROM myapp_person'):
  28. ... print(p)
  29. John Smith
  30. Jane Jones
  31. Of course, this example isn't very exciting -- it's exactly the same as
  32. running ``Person.objects.all()``. However, ``raw()`` has a bunch of other
  33. options that make it very powerful.
  34. .. admonition:: Model table names
  35. Where'd the name of the ``Person`` table come from in that example?
  36. By default, Django figures out a database table name by joining the
  37. model's "app label" -- the name you used in ``manage.py startapp`` -- to
  38. the model's class name, with an underscore between them. In the example
  39. we've assumed that the ``Person`` model lives in an app named ``myapp``,
  40. so its table would be ``myapp_person``.
  41. For more details check out the documentation for the
  42. :attr:`~Options.db_table` option, which also lets you manually set the
  43. database table name.
  44. .. warning::
  45. No checking is done on the SQL statement that is passed in to ``.raw()``.
  46. Django expects that the statement will return a set of rows from the
  47. database, but does nothing to enforce that. If the query does not
  48. return rows, a (possibly cryptic) error will result.
  49. Mapping query fields to model fields
  50. ------------------------------------
  51. ``raw()`` automatically maps fields in the query to fields on the model.
  52. The order of fields in your query doesn't matter. In other words, both
  53. of the following queries work identically::
  54. >>> Person.objects.raw('SELECT id, first_name, last_name, birth_date FROM myapp_person')
  55. ...
  56. >>> Person.objects.raw('SELECT last_name, birth_date, first_name, id FROM myapp_person')
  57. ...
  58. Matching is done by name. This means that you can use SQL's ``AS`` clauses to
  59. map fields in the query to model fields. So if you had some other table that
  60. had ``Person`` data in it, you could easily map it into ``Person`` instances::
  61. >>> Person.objects.raw('''SELECT first AS first_name,
  62. ... last AS last_name,
  63. ... bd AS birth_date,
  64. ... pk as id,
  65. ... FROM some_other_table''')
  66. As long as the names match, the model instances will be created correctly.
  67. Alternatively, you can map fields in the query to model fields using the
  68. ``translations`` argument to ``raw()``. This is a dictionary mapping names of
  69. fields in the query to names of fields on the model. For example, the above
  70. query could also be written::
  71. >>> name_map = {'first': 'first_name', 'last': 'last_name', 'bd': 'birth_date', 'pk': 'id'}
  72. >>> Person.objects.raw('SELECT * FROM some_other_table', translations=name_map)
  73. Index lookups
  74. -------------
  75. ``raw()`` supports indexing, so if you need only the first result you can
  76. write::
  77. >>> first_person = Person.objects.raw('SELECT * from myapp_person')[0]
  78. However, the indexing and slicing are not performed at the database level. If
  79. you have a large number of ``Person`` objects in your database, it is more
  80. efficient to limit the query at the SQL level::
  81. >>> first_person = Person.objects.raw('SELECT * from myapp_person LIMIT 1')[0]
  82. Deferring model fields
  83. ----------------------
  84. Fields may also be left out::
  85. >>> people = Person.objects.raw('SELECT id, first_name FROM myapp_person')
  86. The ``Person`` objects returned by this query will be deferred model instances
  87. (see :meth:`~django.db.models.query.QuerySet.defer()`). This means that the
  88. fields that are omitted from the query will be loaded on demand. For example::
  89. >>> for p in Person.objects.raw('SELECT id, first_name FROM myapp_person'):
  90. ... print(p.first_name, # This will be retrieved by the original query
  91. ... p.last_name) # This will be retrieved on demand
  92. ...
  93. John Smith
  94. Jane Jones
  95. From outward appearances, this looks like the query has retrieved both
  96. the first name and last name. However, this example actually issued 3
  97. queries. Only the first names were retrieved by the raw() query -- the
  98. last names were both retrieved on demand when they were printed.
  99. There is only one field that you can't leave out - the primary key
  100. field. Django uses the primary key to identify model instances, so it
  101. must always be included in a raw query. An ``InvalidQuery`` exception
  102. will be raised if you forget to include the primary key.
  103. Adding annotations
  104. ------------------
  105. You can also execute queries containing fields that aren't defined on the
  106. model. For example, we could use `PostgreSQL's age() function`__ to get a list
  107. of people with their ages calculated by the database::
  108. >>> people = Person.objects.raw('SELECT *, age(birth_date) AS age FROM myapp_person')
  109. >>> for p in people:
  110. ... print("%s is %s." % (p.first_name, p.age))
  111. John is 37.
  112. Jane is 42.
  113. ...
  114. __ http://www.postgresql.org/docs/current/static/functions-datetime.html
  115. Passing parameters into ``raw()``
  116. ---------------------------------
  117. If you need to perform parameterized queries, you can use the ``params``
  118. argument to ``raw()``::
  119. >>> lname = 'Doe'
  120. >>> Person.objects.raw('SELECT * FROM myapp_person WHERE last_name = %s', [lname])
  121. ``params`` is a list or dictionary of parameters. You'll use ``%s``
  122. placeholders in the query string for a list, or ``%(key)s``
  123. placeholders for a dictionary (where ``key`` is replaced by a
  124. dictionary key, of course), regardless of your database engine. Such
  125. placeholders will be replaced with parameters from the ``params``
  126. argument.
  127. .. note:: Dictionary params not supported with SQLite
  128. Dictionary params are not supported with the SQLite backend; with
  129. this backend, you must pass parameters as a list.
  130. .. warning::
  131. **Do not use string formatting on raw queries!**
  132. It's tempting to write the above query as::
  133. >>> query = 'SELECT * FROM myapp_person WHERE last_name = %s' % lname
  134. >>> Person.objects.raw(query)
  135. **Don't.**
  136. Using the ``params`` argument completely protects you from `SQL injection
  137. attacks`__, a common exploit where attackers inject arbitrary SQL into
  138. your database. If you use string interpolation, sooner or later you'll
  139. fall victim to SQL injection. As long as you remember to always use the
  140. ``params`` argument you'll be protected.
  141. __ http://en.wikipedia.org/wiki/SQL_injection
  142. .. versionchanged:: 1.6
  143. In Django 1.5 and earlier, you could pass parameters as dictionaries
  144. when using PostgreSQL or MySQL, although this wasn't documented. Now
  145. you can also do this whem using Oracle, and it is officially supported.
  146. .. _executing-custom-sql:
  147. Executing custom SQL directly
  148. =============================
  149. Sometimes even :meth:`Manager.raw` isn't quite enough: you might need to
  150. perform queries that don't map cleanly to models, or directly execute
  151. ``UPDATE``, ``INSERT``, or ``DELETE`` queries.
  152. In these cases, you can always access the database directly, routing around
  153. the model layer entirely.
  154. The object ``django.db.connection`` represents the default database
  155. connection. To use the database connection, call ``connection.cursor()`` to
  156. get a cursor object. Then, call ``cursor.execute(sql, [params])`` to execute
  157. the SQL and ``cursor.fetchone()`` or ``cursor.fetchall()`` to return the
  158. resulting rows.
  159. For example::
  160. from django.db import connection
  161. def my_custom_sql(self):
  162. cursor = connection.cursor()
  163. cursor.execute("UPDATE bar SET foo = 1 WHERE baz = %s", [self.baz])
  164. cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])
  165. row = cursor.fetchone()
  166. return row
  167. .. versionchanged:: 1.6
  168. In Django 1.5 and earlier, after performing a data changing operation, you
  169. had to call ``transaction.commit_unless_managed()`` to ensure your changes
  170. were committed to the database. Since Django now defaults to database-level
  171. autocommit, this isn't necessary any longer.
  172. Note that if you want to include literal percent signs in the query, you have to
  173. double them in the case you are passing parameters::
  174. cursor.execute("SELECT foo FROM bar WHERE baz = '30%'")
  175. cursor.execute("SELECT foo FROM bar WHERE baz = '30%%' and id = %s", [self.id])
  176. If you are using :doc:`more than one database </topics/db/multi-db>`, you can
  177. use ``django.db.connections`` to obtain the connection (and cursor) for a
  178. specific database. ``django.db.connections`` is a dictionary-like
  179. object that allows you to retrieve a specific connection using its
  180. alias::
  181. from django.db import connections
  182. cursor = connections['my_db_alias'].cursor()
  183. # Your code here...
  184. By default, the Python DB API will return results without their field
  185. names, which means you end up with a ``list`` of values, rather than a
  186. ``dict``. At a small performance cost, you can return results as a
  187. ``dict`` by using something like this::
  188. def dictfetchall(cursor):
  189. "Returns all rows from a cursor as a dict"
  190. desc = cursor.description
  191. return [
  192. dict(zip([col[0] for col in desc], row))
  193. for row in cursor.fetchall()
  194. ]
  195. Here is an example of the difference between the two::
  196. >>> cursor.execute("SELECT id, parent_id from test LIMIT 2");
  197. >>> cursor.fetchall()
  198. ((54360982L, None), (54360880L, None))
  199. >>> cursor.execute("SELECT id, parent_id from test LIMIT 2");
  200. >>> dictfetchall(cursor)
  201. [{'parent_id': None, 'id': 54360982L}, {'parent_id': None, 'id': 54360880L}]
  202. Connections and cursors
  203. -----------------------
  204. ``connection`` and ``cursor`` mostly implement the standard Python DB-API
  205. described in :pep:`249` — except when it comes to :doc:`transaction handling
  206. </topics/db/transactions>`.
  207. If you're not familiar with the Python DB-API, note that the SQL statement in
  208. ``cursor.execute()`` uses placeholders, ``"%s"``, rather than adding
  209. parameters directly within the SQL. If you use this technique, the underlying
  210. database library will automatically escape your parameters as necessary.
  211. Also note that Django expects the ``"%s"`` placeholder, *not* the ``"?"``
  212. placeholder, which is used by the SQLite Python bindings. This is for the sake
  213. of consistency and sanity.
  214. .. versionchanged:: 1.7
  215. :pep:`249` does not state whether a cursor should be usable as a context
  216. manager. Prior to Python 2.7, a cursor was usable as a context manager due
  217. an unexpected behavior in magic method lookups (`Python ticket #9220`_).
  218. Django 1.7 explicitly added support to allow using a cursor as context
  219. manager.
  220. .. _`Python ticket #9220`: http://bugs.python.org/issue9220
  221. Using a cursor as a context manager:
  222. .. code-block:: python
  223. with connection.cursor() as c:
  224. c.execute(...)
  225. is equivalent to:
  226. .. code-block:: python
  227. c = connection.cursor()
  228. try:
  229. c.execute(...)
  230. finally:
  231. c.close()