1 # enkoding.py - show to change client enkoding (and test it works)
2 # -*- encoding: utf8 -*-
4 # Copyright (C) 2004 Federico Di Gregorio <fog@debian.org>
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
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
16 ## put in DSN your DSN string
20 ## don't modify anything below this line (except for experimenting)
24 import psycopg2.extensions
29 print "Opening connection using dns:", DSN
30 conn = psycopg2.connect(DSN)
31 print "Initial encoding for this connection is", conn.encoding
33 print "\n** This example is supposed to be run in a UNICODE terminal! **\n"
35 print "Available encodings:"
36 encs = psycopg2.extensions.encodings.items()
39 print " ", a, "<->", b
41 print "Using STRING typecaster"
42 print "Setting backend encoding to LATIN1 and executing queries:"
43 conn.set_client_encoding('LATIN1')
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)
52 print "Setting backend encoding to UTF8 and executing queries:"
53 conn.set_client_encoding('UNICODE')
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)
62 print "Using UNICODE typecaster"
63 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
65 print "Setting backend encoding to LATIN1 and executing queries:"
66 conn.set_client_encoding('LATIN1')
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)
75 print "Setting backend encoding to UTF8 and executing queries:"
76 conn.set_client_encoding('UNICODE')
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)
85 print "Executing full UNICODE queries"
87 print "Setting backend encoding to LATIN1 and executing queries:"
88 conn.set_client_encoding('LATIN1')
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)
97 print "Setting backend encoding to UTF8 and executing queries:"
98 conn.set_client_encoding('UNICODE')
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)