@@ -21,16 +21,17 @@ The sqlite3 module was written by Gerhard Häring. It provides a SQL interface
2121compliant with the DB-API 2.0 specification described by :pep: `249 `, and
2222requires SQLite 3.7.15 or newer.
2323
24- To use the module, you must first create a :class: `Connection ` object that
24+ To use the module, start by creating a :class: `Connection ` object that
2525represents the database. Here the data will be stored in the
2626:file: `example.db ` file::
2727
2828 import sqlite3
2929 con = sqlite3.connect('example.db')
3030
31- You can also supply the special name ``:memory: `` to create a database in RAM.
31+ The special path name ``:memory: `` can be provided to create a temporary
32+ database in RAM.
3233
33- Once you have a :class: `Connection `, you can create a :class: `Cursor ` object
34+ Once a :class: `Connection ` has been established, create a :class: `Cursor ` object
3435and call its :meth: `~Cursor.execute ` method to perform SQL commands::
3536
3637 cur = con.cursor()
@@ -49,16 +50,17 @@ and call its :meth:`~Cursor.execute` method to perform SQL commands::
4950 # Just be sure any changes have been committed or they will be lost.
5051 con.close()
5152
52- The data you've saved is persistent and is available in subsequent sessions::
53+ The saved data is persistent: it can be reloaded in a subsequent session even
54+ after restarting the Python interpreter::
5355
5456 import sqlite3
5557 con = sqlite3.connect('example.db')
5658 cur = con.cursor()
5759
58- To retrieve data after executing a SELECT statement, you can either treat the
59- cursor as an :term: `iterator `, call the cursor's :meth: `~Cursor.fetchone ` method to
60- retrieve a single matching row, or call :meth: `~Cursor.fetchall ` to get a list of the
61- matching rows.
60+ To retrieve data after executing a SELECT statement, either treat the cursor as
61+ an :term: `iterator `, call the cursor's :meth: `~Cursor.fetchone ` method to
62+ retrieve a single matching row, or call :meth: `~Cursor.fetchall ` to get a list
63+ of the matching rows.
6264
6365This example uses the iterator form::
6466
@@ -73,27 +75,27 @@ This example uses the iterator form::
7375
7476.. _sqlite3-placeholders :
7577
76- Usually your SQL operations will need to use values from Python variables. You
77- shouldn't assemble your query using Python's string operations because doing so
78- is insecure; it makes your program vulnerable to an SQL injection attack
79- (see the `xkcd webcomic <https://xkcd.com/327/ >`_ for a humorous example of
80- what can go wrong)::
78+ SQL operations usually need to use values from Python variables. However,
79+ beware of using Python's string operations to assemble queries, as they
80+ are vulnerable to SQL injection attacks (see the `xkcd webcomic
81+ <https://xkcd.com/327/> `_ for a humorous example of what can go wrong)::
8182
8283 # Never do this -- insecure!
8384 symbol = 'RHAT'
8485 cur.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
8586
86- Instead, use the DB-API's parameter substitution. Put a placeholder wherever
87- you want to use a value, and then provide a tuple of values as the second
88- argument to the cursor's :meth: `~Cursor.execute ` method. An SQL statement may
87+ Instead, use the DB-API's parameter substitution. To insert a variable into a
88+ query string, use a placeholder in the string, and substitute the actual values
89+ into the query by providing them as a :class: `tuple ` of values to the second
90+ argument of the cursor's :meth: `~Cursor.execute ` method. An SQL statement may
8991use one of two kinds of placeholders: question marks (qmark style) or named
9092placeholders (named style). For the qmark style, ``parameters `` must be a
9193:term: `sequence <sequence> `. For the named style, it can be either a
9294:term: `sequence <sequence> ` or :class: `dict ` instance. The length of the
9395:term: `sequence <sequence> ` must match the number of placeholders, or a
9496:exc: `ProgrammingError ` is raised. If a :class: `dict ` is given, it must contain
95- keys for all named parameters. Any extra items are ignored. Here's an example
96- of both styles:
97+ keys for all named parameters. Any extra items are ignored. Here's an example of
98+ both styles:
9799
98100.. literalinclude :: ../includes/sqlite3/execute_1.py
99101
0 commit comments