cpython/Doc/includes/sqlite3/text_factory.py
Miss Islington (bot) 92450f23c6
sqlite3: Modernize documentation around unicode and bytes. (GH-28652) (GH-28695)
(cherry picked from commit 1dac95c814)

Co-authored-by: Julien Palard <julien@palard.fr>
2021-10-05 23:04:02 +02:00

29 lines
775 B
Python

import sqlite3
con = sqlite3.connect(":memory:")
cur = con.cursor()
AUSTRIA = "Österreich"
# by default, rows are returned as str
cur.execute("select ?", (AUSTRIA,))
row = cur.fetchone()
assert row[0] == AUSTRIA
# but we can make sqlite3 always return bytestrings ...
con.text_factory = bytes
cur.execute("select ?", (AUSTRIA,))
row = cur.fetchone()
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 appends "foo" to all strings
con.text_factory = lambda x: x.decode("utf-8") + "foo"
cur.execute("select ?", ("bar",))
row = cur.fetchone()
assert row[0] == "barfoo"
con.close()