Skip to content

Commit 9e06065

Browse files
committed
Docs: Named parameters docs nad usage
1 parent 61a5d28 commit 9e06065

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

docs/query.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,33 @@ characters appear, in the order they appear.
5454
Always use the parameter interpolation feature of the client library to
5555
guard against malicious input, as demonstrated in the example above.
5656

57+
Named parameters
58+
----------------
59+
60+
For queries with many parameters or repeated values, named parameters improve
61+
readability. Pass a :class:`py:dict` as the second argument using
62+
``%(name)s`` placeholders:
63+
64+
>>> cursor.execute(
65+
... "INSERT INTO locations (name, date, kind, position) "
66+
... "VALUES (%(name)s, %(date)s, %(kind)s, %(pos)s)",
67+
... {"name": "Einstein Cross", "date": "2007-03-11", "kind": "Quasar", "pos": 7})
68+
69+
The same parameter name may appear multiple times in the query:
70+
71+
>>> cursor.execute(
72+
... "SELECT * FROM locations WHERE name = %(q)s OR kind = %(q)s",
73+
... {"q": "Quasar"})
74+
75+
The client converts the ``%(name)s`` placeholders to positional ``?`` markers
76+
before sending the query to CrateDB, so no server-side changes are required.
77+
78+
.. NOTE::
79+
80+
Named parameters are not yet supported by ``executemany()``. Use
81+
positional ``?`` placeholders with a :class:`py:list` of tuples for bulk
82+
operations.
83+
5784
Bulk inserts
5885
------------
5986

0 commit comments

Comments
 (0)