]> granicus.if.org Git - python/commitdiff
Issue #13491: Fix many errors in sqlite3 documentation
authorPetri Lehtinen <petri@digip.org>
Wed, 15 Feb 2012 20:17:21 +0000 (22:17 +0200)
committerPetri Lehtinen <petri@digip.org>
Wed, 15 Feb 2012 20:21:01 +0000 (22:21 +0200)
Initial patch by Johannes Vogel.

Doc/includes/sqlite3/converter_point.py
Doc/includes/sqlite3/execute_1.py
Doc/includes/sqlite3/execute_2.py [deleted file]
Doc/includes/sqlite3/executemany_2.py
Doc/includes/sqlite3/md5func.py
Doc/includes/sqlite3/rowclass.py
Doc/includes/sqlite3/text_factory.py
Doc/library/sqlite3.rst
Misc/ACKS
Misc/NEWS

index a8861bcf2e8f4e19ee7c8ee99abe16f3b78a053d..5df828e3361246fb57d5f891d3327fa7c08532e3 100644 (file)
@@ -8,10 +8,10 @@ class Point:
         return "(%f;%f)" % (self.x, self.y)
 
 def adapt_point(point):
-    return "%f;%f" % (point.x, point.y)
+    return ("%f;%f" % (point.x, point.y)).encode('ascii')
 
 def convert_point(s):
-    x, y = list(map(float, s.split(";")))
+    x, y = list(map(float, s.split(b";")))
     return Point(x, y)
 
 # Register the adapter
index 3d08840b86c2d0ef56b65b48824eb41ca734aadf..f864a8984e4e9002343b3ad167653b90da921ec9 100644 (file)
@@ -1,11 +1,16 @@
 import sqlite3
 
-con = sqlite3.connect("mydb")
-
+con = sqlite3.connect(":memory:")
 cur = con.cursor()
+cur.execute("create table people (name_last, age)")
 
 who = "Yeltsin"
 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())
diff --git a/Doc/includes/sqlite3/execute_2.py b/Doc/includes/sqlite3/execute_2.py
deleted file mode 100644 (file)
index 84734f9..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-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())
index 518cd9430d2dd1ab65ae9628f199be9b1f3892ae..527358ebc28e1ff046e70e70ac662b2b0e760a86 100644 (file)
@@ -1,8 +1,8 @@
 import sqlite3
+import string
 
 def char_generator():
-    import string
-    for c in string.letters[:26]:
+    for c in string.ascii_lowercase:
         yield (c,)
 
 con = sqlite3.connect(":memory:")
index b7bc05b379da79af60b2cbf63a44c3ecf2f6aa5a..0056b2d6ce84efb927d1e391ab61bcf367c1cd0e 100644 (file)
@@ -7,5 +7,5 @@ def md5sum(t):
 con = sqlite3.connect(":memory:")
 con.create_function("md5", 1, md5sum)
 cur = con.cursor()
-cur.execute("select md5(?)", ("foo",))
+cur.execute("select md5(?)", (b"foo",))
 print(cur.fetchone()[0])
index 3fa0b873890da1fd00f423d313a8aa3e213bf3b1..92b5ad60cb5791908227b7d217bf8c7936b58a5b 100644 (file)
@@ -1,12 +1,12 @@
 import sqlite3
 
-con = sqlite3.connect("mydb")
+con = sqlite3.connect(":memory:")
 con.row_factory = sqlite3.Row
 
 cur = con.cursor()
-cur.execute("select name_last, age from people")
+cur.execute("select 'John' as name, 42 as age")
 for row in cur:
-    assert row[0] == row["name_last"]
-    assert row["name_last"] == row["nAmE_lAsT"]
+    assert row[0] == row["name"]
+    assert row["name"] == row["nAmE"]
     assert row[1] == row["age"]
     assert row[1] == row["AgE"]
index 22c29700866846b339b0b40ed7b80b729e7af6c0..5f96cdb58da1abb56702b4522cd28f578f95ca13 100644 (file)
@@ -3,9 +3,6 @@ import sqlite3
 con = sqlite3.connect(":memory:")
 cur = con.cursor()
 
-# Create the table
-con.execute("create table person(lastname, firstname)")
-
 AUSTRIA = "\xd6sterreich"
 
 # by default, rows are returned as Unicode
@@ -14,30 +11,17 @@ row = cur.fetchone()
 assert row[0] == AUSTRIA
 
 # but we can make sqlite3 always return bytestrings ...
-con.text_factory = str
+con.text_factory = bytes
 cur.execute("select ?", (AUSTRIA,))
 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
 # database ...
 assert row[0] == AUSTRIA.encode("utf-8")
 
 # we can also implement a custom text_factory ...
-# here we implement one that will ignore Unicode characters that cannot be
-# decoded from UTF-8
-con.text_factory = lambda x: str(x, "utf-8", "ignore")
-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",))
+# here we implement one that appends "foo" to all strings
+con.text_factory = lambda x: x.decode("utf-8") + "foo"
+cur.execute("select ?", ("bar",))
 row = cur.fetchone()
-assert type(row[0]) == str
+assert row[0] == "barfoo"
index 786bb042907696c2d938d186be3338aba13902a8..350950cbc439eac70efc3f16b21c9589a636cc7a 100644 (file)
@@ -472,14 +472,10 @@ Cursor Objects
    kinds of placeholders: question marks (qmark style) and named placeholders
    (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
 
-   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
    more than one statement with it, it will raise a Warning. Use
    :meth:`executescript` if you want to execute multiple SQL statements with one
@@ -761,7 +757,7 @@ and constructs a :class:`Point` object from it.
 ::
 
    def convert_point(s):
-       x, y = map(float, s.split(";"))
+       x, y = map(float, s.split(b";"))
        return Point(x, y)
 
 Now you need to make the :mod:`sqlite3` module know that what you select from
index f79ecedd08d799d63abc41478db34c202840eb3a..ad276e899bff748dff3fd10f25f34b7088cb094e 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -956,6 +956,7 @@ Kannan Vijayan
 Kurt Vile
 Norman Vine
 Frank Visser
+Johannes Vogel
 Sjoerd de Vries
 Niki W. Waibel
 Wojtek Walczak
index a67a26a90775bdb0a6bc3c4ddd8a298b78abdfc2..81ec44b13de140e18cd287d20b5d63ddb61465b9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -524,6 +524,9 @@ Extension Modules
 Documentation
 -------------
 
+- Issue #13491: Fix many errors in sqlite3 documentation. Initial
+  patch by Johannes Vogel.
+
 - Issue #13402: Document absoluteness of sys.executable.
 
 - Issue #13883: PYTHONCASEOK also used on OS X and OS/2.