Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / psycopg2 / examples / encoding.py
1 # enkoding.py - show to change client enkoding (and test it works)
2 # -*- encoding: utf8 -*-
3 #
4 # Copyright (C) 2004 Federico Di Gregorio  <fog@debian.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by the
8 # Free Software Foundation; either version 2, or (at your option) any later
9 # version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 # for more details.
15
16 ## put in DSN your DSN string
17
18 DSN = 'dbname=test'
19
20 ## don't modify anything below this line (except for experimenting)
21
22 import sys
23 import psycopg2
24 import psycopg2.extensions
25
26 if len(sys.argv) > 1:
27     DSN = sys.argv[1]
28
29 print "Opening connection using dns:", DSN
30 conn = psycopg2.connect(DSN)
31 print "Initial encoding for this connection is", conn.encoding
32
33 print "\n** This example is supposed to be run in a UNICODE terminal! **\n"
34
35 print "Available encodings:"
36 encs = psycopg2.extensions.encodings.items()
37 encs.sort()
38 for a, b in encs:
39     print " ", a, "<->", b
40
41 print "Using STRING typecaster"    
42 print "Setting backend encoding to LATIN1 and executing queries:"
43 conn.set_client_encoding('LATIN1')
44 curs = conn.cursor()
45 curs.execute("SELECT %s::TEXT AS foo", ('àèìòù',))
46 x = curs.fetchone()[0]
47 print "  ->", unicode(x, 'latin-1').encode('utf-8'), type(x)
48 curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
49 x = curs.fetchone()[0]
50 print "  ->", unicode(x, 'latin-1').encode('utf-8'), type(x)
51
52 print "Setting backend encoding to UTF8 and executing queries:"
53 conn.set_client_encoding('UNICODE')
54 curs = conn.cursor()
55 curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù'.encode('utf-8'),))
56 x = curs.fetchone()[0]
57 print "  ->", x, type(x)
58 curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
59 x = curs.fetchone()[0]
60 print "  ->", x, type(x)
61
62 print "Using UNICODE typecaster"
63 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
64
65 print "Setting backend encoding to LATIN1 and executing queries:"
66 conn.set_client_encoding('LATIN1')
67 curs = conn.cursor()
68 curs.execute("SELECT %s::TEXT AS foo", ('àèìòù',))
69 x = curs.fetchone()[0]
70 print "  ->", x.encode('utf-8'), ":", type(x)
71 curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
72 x = curs.fetchone()[0]
73 print "  ->", x.encode('utf-8'), ":", type(x)
74
75 print "Setting backend encoding to UTF8 and executing queries:"
76 conn.set_client_encoding('UNICODE')
77 curs = conn.cursor()
78 curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù'.encode('utf-8'),))
79 x = curs.fetchone()[0]
80 print "  ->", x.encode('utf-8'), ":", type(x)
81 curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
82 x = curs.fetchone()[0]
83 print "  ->", x.encode('utf-8'), ":", type(x)
84
85 print "Executing full UNICODE queries"
86
87 print "Setting backend encoding to LATIN1 and executing queries:"
88 conn.set_client_encoding('LATIN1')
89 curs = conn.cursor()
90 curs.execute(u"SELECT %s::TEXT AS foo", ('àèìòù',))
91 x = curs.fetchone()[0]
92 print "  ->", x.encode('utf-8'), ":", type(x)
93 curs.execute(u"SELECT %s::TEXT AS foo", (u'àèìòù',))
94 x = curs.fetchone()[0]
95 print "  ->", x.encode('utf-8'), ":", type(x)
96
97 print "Setting backend encoding to UTF8 and executing queries:"
98 conn.set_client_encoding('UNICODE')
99 curs = conn.cursor()
100 curs.execute(u"SELECT %s::TEXT AS foo", (u'àèìòù'.encode('utf-8'),))
101 x = curs.fetchone()[0]
102 print "  ->", x.encode('utf-8'), ":", type(x)
103 curs.execute(u"SELECT %s::TEXT AS foo", (u'àèìòù',))
104 x = curs.fetchone()[0]
105 print "  ->", x.encode('utf-8'), ":", type(x)