Skip to content

Commit 1ca9395

Browse files
committed
Issue python#13491: Fix many errors in sqlite3 documentation
Initial patch by Johannes Vogel.
1 parent 2640b52 commit 1ca9395

10 files changed

Lines changed: 29 additions & 52 deletions

File tree

Doc/includes/sqlite3/converter_point.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ def __repr__(self):
88
return "(%f;%f)" % (self.x, self.y)
99

1010
def adapt_point(point):
11-
return "%f;%f" % (point.x, point.y)
11+
return ("%f;%f" % (point.x, point.y)).encode('ascii')
1212

1313
def convert_point(s):
14-
x, y = list(map(float, s.split(";")))
14+
x, y = list(map(float, s.split(b";")))
1515
return Point(x, y)
1616

1717
# Register the adapter

Doc/includes/sqlite3/execute_1.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import sqlite3
22

3-
con = sqlite3.connect("mydb")
4-
3+
con = sqlite3.connect(":memory:")
54
cur = con.cursor()
5+
cur.execute("create table people (name_last, age)")
66

77
who = "Yeltsin"
88
age = 72
99

10-
cur.execute("select name_last, age from people where name_last=? and age=?", (who, age))
10+
# This is the qmark style:
11+
cur.execute("insert into people values (?, ?)", (who, age))
12+
13+
# And this is the named style:
14+
cur.execute("select * from people where name_last=:who and age=:age", {"who": who, "age": age})
15+
1116
print(cur.fetchone())

Doc/includes/sqlite3/execute_2.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

Doc/includes/sqlite3/executemany_2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import sqlite3
2+
import string
23

34
def char_generator():
4-
import string
5-
for c in string.letters[:26]:
5+
for c in string.ascii_lowercase:
66
yield (c,)
77

88
con = sqlite3.connect(":memory:")

Doc/includes/sqlite3/md5func.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ def md5sum(t):
77
con = sqlite3.connect(":memory:")
88
con.create_function("md5", 1, md5sum)
99
cur = con.cursor()
10-
cur.execute("select md5(?)", ("foo",))
10+
cur.execute("select md5(?)", (b"foo",))
1111
print(cur.fetchone()[0])

Doc/includes/sqlite3/rowclass.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import sqlite3
22

3-
con = sqlite3.connect("mydb")
3+
con = sqlite3.connect(":memory:")
44
con.row_factory = sqlite3.Row
55

66
cur = con.cursor()
7-
cur.execute("select name_last, age from people")
7+
cur.execute("select 'John' as name, 42 as age")
88
for row in cur:
9-
assert row[0] == row["name_last"]
10-
assert row["name_last"] == row["nAmE_lAsT"]
9+
assert row[0] == row["name"]
10+
assert row["name"] == row["nAmE"]
1111
assert row[1] == row["age"]
1212
assert row[1] == row["AgE"]

Doc/includes/sqlite3/text_factory.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
con = sqlite3.connect(":memory:")
44
cur = con.cursor()
55

6-
# Create the table
7-
con.execute("create table person(lastname, firstname)")
8-
96
AUSTRIA = "\xd6sterreich"
107

118
# by default, rows are returned as Unicode
@@ -14,30 +11,17 @@
1411
assert row[0] == AUSTRIA
1512

1613
# but we can make sqlite3 always return bytestrings ...
17-
con.text_factory = str
14+
con.text_factory = bytes
1815
cur.execute("select ?", (AUSTRIA,))
1916
row = cur.fetchone()
20-
assert type(row[0]) == str
17+
assert type(row[0]) is bytes
2118
# the bytestrings will be encoded in UTF-8, unless you stored garbage in the
2219
# database ...
2320
assert row[0] == AUSTRIA.encode("utf-8")
2421

2522
# we can also implement a custom text_factory ...
26-
# here we implement one that will ignore Unicode characters that cannot be
27-
# decoded from UTF-8
28-
con.text_factory = lambda x: str(x, "utf-8", "ignore")
29-
cur.execute("select ?", ("this is latin1 and would normally create errors" +
30-
"\xe4\xf6\xfc".encode("latin1"),))
31-
row = cur.fetchone()
32-
assert type(row[0]) == str
33-
34-
# sqlite3 offers a built-in optimized text_factory that will return bytestring
35-
# objects, if the data is in ASCII only, and otherwise return unicode objects
36-
con.text_factory = sqlite3.OptimizedUnicode
37-
cur.execute("select ?", (AUSTRIA,))
38-
row = cur.fetchone()
39-
assert type(row[0]) == str
40-
41-
cur.execute("select ?", ("Germany",))
23+
# here we implement one that appends "foo" to all strings
24+
con.text_factory = lambda x: x.decode("utf-8") + "foo"
25+
cur.execute("select ?", ("bar",))
4226
row = cur.fetchone()
43-
assert type(row[0]) == str
27+
assert row[0] == "barfoo"

Doc/library/sqlite3.rst

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -472,14 +472,10 @@ Cursor Objects
472472
kinds of placeholders: question marks (qmark style) and named placeholders
473473
(named style).
474474

475-
This example shows how to use parameters with qmark style:
475+
Here's an example of both styles:
476476

477477
.. literalinclude:: ../includes/sqlite3/execute_1.py
478478

479-
This example shows how to use the named style:
480-
481-
.. literalinclude:: ../includes/sqlite3/execute_2.py
482-
483479
:meth:`execute` will only execute a single SQL statement. If you try to execute
484480
more than one statement with it, it will raise a Warning. Use
485481
:meth:`executescript` if you want to execute multiple SQL statements with one
@@ -761,7 +757,7 @@ and constructs a :class:`Point` object from it.
761757
::
762758

763759
def convert_point(s):
764-
x, y = map(float, s.split(";"))
760+
x, y = map(float, s.split(b";"))
765761
return Point(x, y)
766762

767763
Now you need to make the :mod:`sqlite3` module know that what you select from

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ Kannan Vijayan
956956
Kurt Vile
957957
Norman Vine
958958
Frank Visser
959+
Johannes Vogel
959960
Sjoerd de Vries
960961
Niki W. Waibel
961962
Wojtek Walczak

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,9 @@ Extension Modules
524524
Documentation
525525
-------------
526526

527+
- Issue #13491: Fix many errors in sqlite3 documentation. Initial
528+
patch by Johannes Vogel.
529+
527530
- Issue #13402: Document absoluteness of sys.executable.
528531

529532
- Issue #13883: PYTHONCASEOK also used on OS X and OS/2.

0 commit comments

Comments
 (0)