Skip to content

Commit 6c5a312

Browse files
Erlend Egeberg AaslandjacobtylerwallsAlexWaygood
authored
bpo-45677: Reword first section of sqlite3 docs (#29326)
* bpo-45677: Avoid addressing the reader as 'you' in sqlite3 docs * Adjust wording * Adjust wording again * Typo * Update Doc/library/sqlite3.rst Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com> * Address review: adjust wording * Update Doc/library/sqlite3.rst Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> * Update Lib/sqlite3/__init__.py Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> * Update Doc/library/sqlite3.rst Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> * Update Doc/library/sqlite3.rst Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> * Update Lib/sqlite3/__init__.py Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> * Update Doc/library/sqlite3.rst Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> * Apply Alex' suggestion, and apply 80 char limit to PR * Minor adjustment Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent ec382fa commit 6c5a312

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

Doc/library/sqlite3.rst

+20-18
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ The sqlite3 module was written by Gerhard Häring. It provides a SQL interface
2121
compliant with the DB-API 2.0 specification described by :pep:`249`, and
2222
requires 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
2525
represents 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
3435
and 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

6365
This 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
8991
use one of two kinds of placeholders: question marks (qmark style) or named
9092
placeholders (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

Lib/sqlite3/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@
2424
The sqlite3 extension module provides a DB-API 2.0 (PEP 249) compilant
2525
interface to the SQLite library, and requires SQLite 3.7.15 or newer.
2626
27-
To use the module, you must first create a database Connection object:
27+
To use the module, start by creating a database Connection object:
2828
2929
import sqlite3
3030
cx = sqlite3.connect("test.db") # test.db will be created or opened
3131
32-
You can also use the special database name ":memory:" to connect to a transient
32+
The special path name ":memory:" can be provided to connect to a transient
3333
in-memory database:
3434
3535
cx = sqlite3.connect(":memory:") # connect to a database in RAM
3636
37-
Once you have a Connection object, you can create a Cursor object and call its
38-
execute() method to perform SQL queries:
37+
Once a connection has been established, create a Cursor object and call
38+
its execute() method to perform SQL queries:
3939
4040
cu = cx.cursor()
4141

0 commit comments

Comments
 (0)