2007-08-15 11:28:22 -03:00
|
|
|
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
|
|
|
|
cur.execute("select ?", (AUSTRIA,))
|
|
|
|
row = cur.fetchone()
|
|
|
|
assert row[0] == AUSTRIA
|
|
|
|
|
Merged revisions 72661,72675-72677,72679,72712,72801,72820 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r72661 | georg.brandl | 2009-05-15 10:03:03 +0200 (Fr, 15 Mai 2009) | 1 line
Fix example output for doctest-like demos.
........
r72675 | georg.brandl | 2009-05-16 13:13:21 +0200 (Sa, 16 Mai 2009) | 1 line
#6034: clarify __reversed__ doc.
........
r72676 | georg.brandl | 2009-05-16 13:14:46 +0200 (Sa, 16 Mai 2009) | 1 line
#6025: fix signature of parse().
........
r72677 | georg.brandl | 2009-05-16 13:18:55 +0200 (Sa, 16 Mai 2009) | 1 line
#6009: undocument default argument of Option as deprecated.
........
r72679 | georg.brandl | 2009-05-16 13:24:41 +0200 (Sa, 16 Mai 2009) | 1 line
Fix about and bugs pages to match real workflow.
........
r72712 | georg.brandl | 2009-05-17 10:55:00 +0200 (So, 17 Mai 2009) | 1 line
#5935: mention that BROWSER is looked for in PATH.
........
r72801 | georg.brandl | 2009-05-20 20:31:14 +0200 (Mi, 20 Mai 2009) | 1 line
#6055: refer to "sqlite3" consistently.
........
r72820 | georg.brandl | 2009-05-22 09:23:32 +0200 (Fr, 22 Mai 2009) | 1 line
Use raise X(y).
........
2009-05-25 18:13:36 -03:00
|
|
|
# but we can make sqlite3 always return bytestrings ...
|
2007-08-15 11:28:22 -03:00
|
|
|
con.text_factory = str
|
|
|
|
cur.execute("select ?", (AUSTRIA,))
|
|
|
|
row = cur.fetchone()
|
|
|
|
assert type(row[0]) == str
|
|
|
|
# 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")
|
Merged revisions 72661,72675-72677,72679,72712,72801,72820 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r72661 | georg.brandl | 2009-05-15 10:03:03 +0200 (Fr, 15 Mai 2009) | 1 line
Fix example output for doctest-like demos.
........
r72675 | georg.brandl | 2009-05-16 13:13:21 +0200 (Sa, 16 Mai 2009) | 1 line
#6034: clarify __reversed__ doc.
........
r72676 | georg.brandl | 2009-05-16 13:14:46 +0200 (Sa, 16 Mai 2009) | 1 line
#6025: fix signature of parse().
........
r72677 | georg.brandl | 2009-05-16 13:18:55 +0200 (Sa, 16 Mai 2009) | 1 line
#6009: undocument default argument of Option as deprecated.
........
r72679 | georg.brandl | 2009-05-16 13:24:41 +0200 (Sa, 16 Mai 2009) | 1 line
Fix about and bugs pages to match real workflow.
........
r72712 | georg.brandl | 2009-05-17 10:55:00 +0200 (So, 17 Mai 2009) | 1 line
#5935: mention that BROWSER is looked for in PATH.
........
r72801 | georg.brandl | 2009-05-20 20:31:14 +0200 (Mi, 20 Mai 2009) | 1 line
#6055: refer to "sqlite3" consistently.
........
r72820 | georg.brandl | 2009-05-22 09:23:32 +0200 (Fr, 22 Mai 2009) | 1 line
Use raise X(y).
........
2009-05-25 18:13:36 -03:00
|
|
|
cur.execute("select ?", ("this is latin1 and would normally create errors" +
|
|
|
|
"\xe4\xf6\xfc".encode("latin1"),))
|
2007-08-15 11:28:22 -03:00
|
|
|
row = cur.fetchone()
|
|
|
|
assert type(row[0]) == str
|
|
|
|
|
2009-07-26 11:54:51 -03:00
|
|
|
# sqlite3 offers a built-in optimized text_factory that will return bytestring
|
2007-08-15 11:28:22 -03:00
|
|
|
# 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()
|
|
|
|
assert type(row[0]) == str
|