Commit 1ca93954 authored by Petri Lehtinen's avatar Petri Lehtinen

Issue #13491: Fix many errors in sqlite3 documentation

Initial patch by Johannes Vogel.
parent 2640b522
...@@ -8,10 +8,10 @@ class Point: ...@@ -8,10 +8,10 @@ class Point:
return "(%f;%f)" % (self.x, self.y) return "(%f;%f)" % (self.x, self.y)
def adapt_point(point): def adapt_point(point):
return "%f;%f" % (point.x, point.y) return ("%f;%f" % (point.x, point.y)).encode('ascii')
def convert_point(s): def convert_point(s):
x, y = list(map(float, s.split(";"))) x, y = list(map(float, s.split(b";")))
return Point(x, y) return Point(x, y)
# Register the adapter # Register the adapter
......
import sqlite3 import sqlite3
con = sqlite3.connect("mydb") con = sqlite3.connect(":memory:")
cur = con.cursor() cur = con.cursor()
cur.execute("create table people (name_last, age)")
who = "Yeltsin" who = "Yeltsin"
age = 72 age = 72
cur.execute("select name_last, age from people where name_last=? and age=?", (who, age)) # This is the qmark style:
cur.execute("insert into people values (?, ?)", (who, age))
# And this is the named style:
cur.execute("select * from people where name_last=:who and age=:age", {"who": who, "age": age})
print(cur.fetchone()) print(cur.fetchone())
import sqlite3
con = sqlite3.connect("mydb")
cur = con.cursor()
who = "Yeltsin"
age = 72
cur.execute("select name_last, age from people where name_last=:who and age=:age",
{"who": who, "age": age})
print(cur.fetchone())
import sqlite3 import sqlite3
import string
def char_generator(): def char_generator():
import string for c in string.ascii_lowercase:
for c in string.letters[:26]:
yield (c,) yield (c,)
con = sqlite3.connect(":memory:") con = sqlite3.connect(":memory:")
......
...@@ -7,5 +7,5 @@ def md5sum(t): ...@@ -7,5 +7,5 @@ def md5sum(t):
con = sqlite3.connect(":memory:") con = sqlite3.connect(":memory:")
con.create_function("md5", 1, md5sum) con.create_function("md5", 1, md5sum)
cur = con.cursor() cur = con.cursor()
cur.execute("select md5(?)", ("foo",)) cur.execute("select md5(?)", (b"foo",))
print(cur.fetchone()[0]) print(cur.fetchone()[0])
import sqlite3 import sqlite3
con = sqlite3.connect("mydb") con = sqlite3.connect(":memory:")
con.row_factory = sqlite3.Row con.row_factory = sqlite3.Row
cur = con.cursor() cur = con.cursor()
cur.execute("select name_last, age from people") cur.execute("select 'John' as name, 42 as age")
for row in cur: for row in cur:
assert row[0] == row["name_last"] assert row[0] == row["name"]
assert row["name_last"] == row["nAmE_lAsT"] assert row["name"] == row["nAmE"]
assert row[1] == row["age"] assert row[1] == row["age"]
assert row[1] == row["AgE"] assert row[1] == row["AgE"]
...@@ -3,9 +3,6 @@ import sqlite3 ...@@ -3,9 +3,6 @@ import sqlite3
con = sqlite3.connect(":memory:") con = sqlite3.connect(":memory:")
cur = con.cursor() cur = con.cursor()
# Create the table
con.execute("create table person(lastname, firstname)")
AUSTRIA = "\xd6sterreich" AUSTRIA = "\xd6sterreich"
# by default, rows are returned as Unicode # by default, rows are returned as Unicode
...@@ -14,30 +11,17 @@ row = cur.fetchone() ...@@ -14,30 +11,17 @@ row = cur.fetchone()
assert row[0] == AUSTRIA assert row[0] == AUSTRIA
# but we can make sqlite3 always return bytestrings ... # but we can make sqlite3 always return bytestrings ...
con.text_factory = str con.text_factory = bytes
cur.execute("select ?", (AUSTRIA,)) cur.execute("select ?", (AUSTRIA,))
row = cur.fetchone() row = cur.fetchone()
assert type(row[0]) == str assert type(row[0]) is bytes
# the bytestrings will be encoded in UTF-8, unless you stored garbage in the # the bytestrings will be encoded in UTF-8, unless you stored garbage in the
# database ... # database ...
assert row[0] == AUSTRIA.encode("utf-8") assert row[0] == AUSTRIA.encode("utf-8")
# we can also implement a custom text_factory ... # we can also implement a custom text_factory ...
# here we implement one that will ignore Unicode characters that cannot be # here we implement one that appends "foo" to all strings
# decoded from UTF-8 con.text_factory = lambda x: x.decode("utf-8") + "foo"
con.text_factory = lambda x: str(x, "utf-8", "ignore") cur.execute("select ?", ("bar",))
cur.execute("select ?", ("this is latin1 and would normally create errors" +
"\xe4\xf6\xfc".encode("latin1"),))
row = cur.fetchone()
assert type(row[0]) == str
# sqlite3 offers a built-in optimized text_factory that will return bytestring
# objects, if the data is in ASCII only, and otherwise return unicode objects
con.text_factory = sqlite3.OptimizedUnicode
cur.execute("select ?", (AUSTRIA,))
row = cur.fetchone()
assert type(row[0]) == str
cur.execute("select ?", ("Germany",))
row = cur.fetchone() row = cur.fetchone()
assert type(row[0]) == str assert row[0] == "barfoo"
...@@ -472,14 +472,10 @@ Cursor Objects ...@@ -472,14 +472,10 @@ Cursor Objects
kinds of placeholders: question marks (qmark style) and named placeholders kinds of placeholders: question marks (qmark style) and named placeholders
(named style). (named style).
This example shows how to use parameters with qmark style: Here's an example of both styles:
.. literalinclude:: ../includes/sqlite3/execute_1.py .. literalinclude:: ../includes/sqlite3/execute_1.py
This example shows how to use the named style:
.. literalinclude:: ../includes/sqlite3/execute_2.py
:meth:`execute` will only execute a single SQL statement. If you try to execute :meth:`execute` will only execute a single SQL statement. If you try to execute
more than one statement with it, it will raise a Warning. Use more than one statement with it, it will raise a Warning. Use
:meth:`executescript` if you want to execute multiple SQL statements with one :meth:`executescript` if you want to execute multiple SQL statements with one
...@@ -761,7 +757,7 @@ and constructs a :class:`Point` object from it. ...@@ -761,7 +757,7 @@ and constructs a :class:`Point` object from it.
:: ::
def convert_point(s): def convert_point(s):
x, y = map(float, s.split(";")) x, y = map(float, s.split(b";"))
return Point(x, y) return Point(x, y)
Now you need to make the :mod:`sqlite3` module know that what you select from Now you need to make the :mod:`sqlite3` module know that what you select from
......
...@@ -956,6 +956,7 @@ Kannan Vijayan ...@@ -956,6 +956,7 @@ Kannan Vijayan
Kurt Vile Kurt Vile
Norman Vine Norman Vine
Frank Visser Frank Visser
Johannes Vogel
Sjoerd de Vries Sjoerd de Vries
Niki W. Waibel Niki W. Waibel
Wojtek Walczak Wojtek Walczak
......
...@@ -524,6 +524,9 @@ Extension Modules ...@@ -524,6 +524,9 @@ Extension Modules
Documentation Documentation
------------- -------------
- Issue #13491: Fix many errors in sqlite3 documentation. Initial
patch by Johannes Vogel.
- Issue #13402: Document absoluteness of sys.executable. - Issue #13402: Document absoluteness of sys.executable.
- Issue #13883: PYTHONCASEOK also used on OS X and OS/2. - Issue #13883: PYTHONCASEOK also used on OS X and OS/2.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment