diff options
Diffstat (limited to 'Doc/includes/sqlite3/text_factory.py')
-rw-r--r-- | Doc/includes/sqlite3/text_factory.py | 17 |
1 files changed, 6 insertions, 11 deletions
diff --git a/Doc/includes/sqlite3/text_factory.py b/Doc/includes/sqlite3/text_factory.py index bdffd36..5f96cdb 100644 --- a/Doc/includes/sqlite3/text_factory.py +++ b/Doc/includes/sqlite3/text_factory.py @@ -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,19 +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"),)) +# 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" |