~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Doc/includes/sqlite3/text_factory.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import sqlite3
 
2
 
 
3
con = sqlite3.connect(":memory:")
 
4
cur = con.cursor()
 
5
 
 
6
AUSTRIA = "\xd6sterreich"
 
7
 
 
8
# by default, rows are returned as Unicode
 
9
cur.execute("select ?", (AUSTRIA,))
 
10
row = cur.fetchone()
 
11
assert row[0] == AUSTRIA
 
12
 
 
13
# but we can make sqlite3 always return bytestrings ...
 
14
con.text_factory = bytes
 
15
cur.execute("select ?", (AUSTRIA,))
 
16
row = cur.fetchone()
 
17
assert type(row[0]) is bytes
 
18
# the bytestrings will be encoded in UTF-8, unless you stored garbage in the
 
19
# database ...
 
20
assert row[0] == AUSTRIA.encode("utf-8")
 
21
 
 
22
# we can also implement a custom text_factory ...
 
23
# here we implement one that appends "foo" to all strings
 
24
con.text_factory = lambda x: x.decode("utf-8") + "foo"
 
25
cur.execute("select ?", ("bar",))
 
26
row = cur.fetchone()
 
27
assert row[0] == "barfoo"