You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/getting_started.rst
+76-33Lines changed: 76 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -110,7 +110,6 @@ by executing a ``USE <keyspace>`` query:
110
110
# or you can do this instead
111
111
session.execute('USE users')
112
112
113
-
114
113
Executing Queries
115
114
-----------------
116
115
Now that we have a :class:`.Session` we can begin to execute queries. The simplest
@@ -153,13 +152,45 @@ examples are equivalent:
153
152
If you prefer another result format, such as a ``dict`` per row, you
154
153
can change the :attr:`~.Session.row_factory` attribute.
155
154
156
-
For queries that will be run repeatedly, you should use
157
-
`Prepared statements <#prepared-statements>`_.
155
+
As mentioned in our `Drivers Best Practices Guide <https://docs.datastax.com/en/devapp/doc/devapp/driversBestPractices.html#driversBestPractices__usePreparedStatements>`_,
156
+
it is highly recommended to use `Prepared statements <#prepared-statement>`_ for your
157
+
frequently run queries.
158
+
159
+
.. _prepared-statement:
160
+
161
+
Prepared Statements
162
+
-------------------
163
+
Prepared statements are queries that are parsed by Cassandra and then saved
164
+
for later use. When the driver uses a prepared statement, it only needs to
165
+
send the values of parameters to bind. This lowers network traffic
166
+
and CPU utilization within Cassandra because Cassandra does not have to
167
+
re-parse the query each time.
168
+
169
+
To prepare a query, use :meth:`.Session.prepare()`:
170
+
171
+
.. code-block:: python
172
+
173
+
user_lookup_stmt = session.prepare("SELECT * FROM users WHERE user_id=?")
174
+
175
+
users = []
176
+
for user_id in user_ids_to_query:
177
+
user = session.execute(user_lookup_stmt, [user_id])
178
+
users.append(user)
179
+
180
+
:meth:`~.Session.prepare()` returns a :class:`~.PreparedStatement` instance
181
+
which can be used in place of :class:`~.SimpleStatement` instances or literal
182
+
string queries. It is automatically prepared against all nodes, and the driver
183
+
handles re-preparing against new nodes and restarted nodes when necessary.
184
+
185
+
Note that the placeholders for prepared statements are ``?`` characters. This
186
+
is different than for simple, non-prepared statements (although future versions
187
+
of the driver may use the same placeholders for both).
158
188
159
189
Passing Parameters to CQL Queries
160
190
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
161
-
When executing non-prepared statements, the driver supports two forms of
162
-
parameter place-holders: positional and named.
191
+
Althought it is not recommended, you can also pass parameters to non-prepared
192
+
statements. The driver supports two forms of parameter place-holders: positional
193
+
and named.
163
194
164
195
Positional parameters are used with a ``%s`` placeholder. For example,
165
196
when you execute:
@@ -376,34 +407,6 @@ in a :class:`~.SimpleStatement`:
376
407
consistency_level=ConsistencyLevel.QUORUM)
377
408
session.execute(query, ('John', 42))
378
409
379
-
Prepared Statements
380
-
-------------------
381
-
Prepared statements are queries that are parsed by Cassandra and then saved
382
-
for later use. When the driver uses a prepared statement, it only needs to
383
-
send the values of parameters to bind. This lowers network traffic
384
-
andCPU utilization within Cassandra because Cassandra does not have to
385
-
re-parse the query each time.
386
-
387
-
To prepare a query, use :meth:`.Session.prepare()`:
388
-
389
-
.. code-block:: python
390
-
391
-
user_lookup_stmt= session.prepare("SELECT * FROM users WHERE user_id=?")
0 commit comments