1import sqlite3 2 3con = sqlite3.connect(":memory:") 4cur = con.cursor() 5 6AUSTRIA = u"\xd6sterreich" 7 8# by default, rows are returned as Unicode 9cur.execute("select ?", (AUSTRIA,)) 10row = cur.fetchone() 11assert row[0] == AUSTRIA 12 13# but we can make sqlite3 always return bytestrings ... 14con.text_factory = str 15cur.execute("select ?", (AUSTRIA,)) 16row = cur.fetchone() 17assert type(row[0]) is str 18# the bytestrings will be encoded in UTF-8, unless you stored garbage in the 19# database ... 20assert row[0] == AUSTRIA.encode("utf-8") 21 22# we can also implement a custom text_factory ... 23# here we implement one that will ignore Unicode characters that cannot be 24# decoded from UTF-8 25con.text_factory = lambda x: unicode(x, "utf-8", "ignore") 26cur.execute("select ?", ("this is latin1 and would normally create errors" + 27 u"\xe4\xf6\xfc".encode("latin1"),)) 28row = cur.fetchone() 29assert type(row[0]) is unicode 30 31# sqlite3 offers a built-in optimized text_factory that will return bytestring 32# objects, if the data is in ASCII only, and otherwise return unicode objects 33con.text_factory = sqlite3.OptimizedUnicode 34cur.execute("select ?", (AUSTRIA,)) 35row = cur.fetchone() 36assert type(row[0]) is unicode 37 38cur.execute("select ?", ("Germany",)) 39row = cur.fetchone() 40assert type(row[0]) is str 41