@@ -94,6 +94,12 @@ using :meth:`~Cursor.executemany`::
9494 ... ]
9595 >>> cur.executemany('INSERT INTO stocks VALUES(?, ?, ?, ?, ?)', data)
9696
97+ Notice that we used ``? `` placeholders to bind *data * to the query.
98+ Always use placeholders instead of :ref: `string formatting <tut-formatting >`
99+ to bind Python values to SQL statements,
100+ to avoid `SQL injection attacks `_.
101+ See the :ref: `placeholders how-to <sqlite3-placeholders >` for more details.
102+
97103Then, retrieve the data by iterating over the result of a ``SELECT `` statement::
98104
99105 >>> for row in cur.execute('SELECT * FROM stocks ORDER BY price'):
@@ -104,33 +110,9 @@ Then, retrieve the data by iterating over the result of a ``SELECT`` statement::
104110 ('2006-04-06', 'SELL', 'IBM', 500, 53.0)
105111 ('2006-04-05', 'BUY', 'MSFT', 1000, 72.0)
106112
113+ You've now created an SQLite database using the :mod: `!sqlite3 ` module.
107114
108- .. _sqlite3-placeholders :
109-
110- SQL operations usually need to use values from Python variables. However,
111- beware of using Python's string operations to assemble queries, as they
112- are vulnerable to SQL injection attacks (see the `xkcd webcomic
113- <https://xkcd.com/327/> `_ for a humorous example of what can go wrong)::
114-
115- # Never do this -- insecure!
116- symbol = 'RHAT'
117- cur.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
118-
119- Instead, use the DB-API's parameter substitution. To insert a variable into a
120- query string, use a placeholder in the string, and substitute the actual values
121- into the query by providing them as a :class: `tuple ` of values to the second
122- argument of the cursor's :meth: `~Cursor.execute ` method. An SQL statement may
123- use one of two kinds of placeholders: question marks (qmark style) or named
124- placeholders (named style). For the qmark style, ``parameters `` must be a
125- :term: `sequence <sequence> `. For the named style, it can be either a
126- :term: `sequence <sequence> ` or :class: `dict ` instance. The length of the
127- :term: `sequence <sequence> ` must match the number of placeholders, or a
128- :exc: `ProgrammingError ` is raised. If a :class: `dict ` is given, it must contain
129- keys for all named parameters. Any extra items are ignored. Here's an example of
130- both styles:
131-
132- .. literalinclude :: ../includes/sqlite3/execute_1.py
133-
115+ .. _SQL injection attacks : https://en.wikipedia.org/wiki/SQL_injection
134116
135117.. seealso ::
136118
@@ -1447,6 +1429,36 @@ Python types via :ref:`converters <sqlite3-converters>`.
14471429How-to guides
14481430-------------
14491431
1432+ .. _sqlite3-placeholders :
1433+
1434+ Using placeholders to bind values in SQL queries
1435+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1436+
1437+ SQL operations usually need to use values from Python variables. However,
1438+ beware of using Python's string operations to assemble queries, as they
1439+ are vulnerable to `SQL injection attacks `_ (see the `xkcd webcomic
1440+ <https://xkcd.com/327/> `_ for a humorous example of what can go wrong)::
1441+
1442+ # Never do this -- insecure!
1443+ symbol = 'RHAT'
1444+ cur.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
1445+
1446+ Instead, use the DB-API's parameter substitution. To insert a variable into a
1447+ query string, use a placeholder in the string, and substitute the actual values
1448+ into the query by providing them as a :class: `tuple ` of values to the second
1449+ argument of the cursor's :meth: `~Cursor.execute ` method. An SQL statement may
1450+ use one of two kinds of placeholders: question marks (qmark style) or named
1451+ placeholders (named style). For the qmark style, ``parameters `` must be a
1452+ :term: `sequence <sequence> `. For the named style, it can be either a
1453+ :term: `sequence <sequence> ` or :class: `dict ` instance. The length of the
1454+ :term: `sequence <sequence> ` must match the number of placeholders, or a
1455+ :exc: `ProgrammingError ` is raised. If a :class: `dict ` is given, it must contain
1456+ keys for all named parameters. Any extra items are ignored. Here's an example of
1457+ both styles:
1458+
1459+ .. literalinclude :: ../includes/sqlite3/execute_1.py
1460+
1461+
14501462.. _sqlite3-adapters :
14511463
14521464Using adapters to store custom Python types in SQLite databases
0 commit comments