~psycopg/psycopg/2.0.x

« back to all changes in this revision

Viewing changes to examples/encoding.py

  • Committer: Federico Di Gregorio
  • Date: 2006-03-30 02:20:11 UTC
  • Revision ID: fog-0012b4068eaad7d8b02b4f40e7f1b41e7d75f524
Fixing SVN transition.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# encoding.py - how to change client encoding (and test it works)
2
 
# -*- encoding: latin-1 -*-
 
1
# enkoding.py - show to change client enkoding (and test it works)
 
2
# -*- encoding: utf8 -*-
3
3
#
4
4
# Copyright (C) 2004 Federico Di Gregorio  <fog@debian.org>
5
5
#
19
19
 
20
20
## don't modify anything below this line (except for experimenting)
21
21
 
22
 
import sys, psycopg
23
 
import psycopg.extensions
 
22
import sys
 
23
import psycopg2
 
24
import psycopg2.extensions
24
25
 
25
26
if len(sys.argv) > 1:
26
27
    DSN = sys.argv[1]
27
28
 
28
29
print "Opening connection using dns:", DSN
29
 
conn = psycopg.connect(DSN)
 
30
conn = psycopg2.connect(DSN)
30
31
print "Initial encoding for this connection is", conn.encoding
31
32
 
32
33
print "\n** This example is supposed to be run in a UNICODE terminal! **\n"
33
34
 
34
35
print "Available encodings:"
35
 
for a, b in psycopg.extensions.encodings.items():
 
36
encs = psycopg2.extensions.encodings.items()
 
37
encs.sort()
 
38
for a, b in encs:
36
39
    print " ", a, "<->", b
37
40
 
38
41
print "Using STRING typecaster"    
39
42
print "Setting backend encoding to LATIN1 and executing queries:"
40
43
conn.set_client_encoding('LATIN1')
41
44
curs = conn.cursor()
42
 
curs.execute("SELECT %s::TEXT AS foo", ('�����',))
 
45
curs.execute("SELECT %s::TEXT AS foo", ('àèìòù',))
43
46
x = curs.fetchone()[0]
44
47
print "  ->", unicode(x, 'latin-1').encode('utf-8'), type(x)
45
 
curs.execute("SELECT %s::TEXT AS foo", (u'�����',))
 
48
curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
46
49
x = curs.fetchone()[0]
47
50
print "  ->", unicode(x, 'latin-1').encode('utf-8'), type(x)
48
51
 
49
52
print "Setting backend encoding to UTF8 and executing queries:"
50
53
conn.set_client_encoding('UNICODE')
51
54
curs = conn.cursor()
52
 
curs.execute("SELECT %s::TEXT AS foo", (u'�����'.encode('utf-8'),))
 
55
curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù'.encode('utf-8'),))
53
56
x = curs.fetchone()[0]
54
57
print "  ->", x, type(x)
55
 
curs.execute("SELECT %s::TEXT AS foo", (u'�����',))
 
58
curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
56
59
x = curs.fetchone()[0]
57
60
print "  ->", x, type(x)
58
61
 
59
62
print "Using UNICODE typecaster"
60
 
psycopg.extensions.register_type(psycopg.extensions.UNICODE)
 
63
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
61
64
 
62
65
print "Setting backend encoding to LATIN1 and executing queries:"
63
66
conn.set_client_encoding('LATIN1')
64
67
curs = conn.cursor()
65
 
curs.execute("SELECT %s::TEXT AS foo", ('�����',))
 
68
curs.execute("SELECT %s::TEXT AS foo", ('àèìòù',))
66
69
x = curs.fetchone()[0]
67
70
print "  ->", x.encode('utf-8'), ":", type(x)
68
 
curs.execute("SELECT %s::TEXT AS foo", (u'�����',))
 
71
curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
69
72
x = curs.fetchone()[0]
70
73
print "  ->", x.encode('utf-8'), ":", type(x)
71
74
 
72
75
print "Setting backend encoding to UTF8 and executing queries:"
73
76
conn.set_client_encoding('UNICODE')
74
77
curs = conn.cursor()
75
 
curs.execute("SELECT %s::TEXT AS foo", (u'�����'.encode('utf-8'),))
 
78
curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù'.encode('utf-8'),))
76
79
x = curs.fetchone()[0]
77
80
print "  ->", x.encode('utf-8'), ":", type(x)
78
 
curs.execute("SELECT %s::TEXT AS foo", (u'�����',))
 
81
curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
79
82
x = curs.fetchone()[0]
80
83
print "  ->", x.encode('utf-8'), ":", type(x)
81
84
 
84
87
print "Setting backend encoding to LATIN1 and executing queries:"
85
88
conn.set_client_encoding('LATIN1')
86
89
curs = conn.cursor()
87
 
curs.execute(u"SELECT %s::TEXT AS foo", ('�����',))
 
90
curs.execute(u"SELECT %s::TEXT AS foo", ('àèìòù',))
88
91
x = curs.fetchone()[0]
89
92
print "  ->", x.encode('utf-8'), ":", type(x)
90
 
curs.execute(u"SELECT %s::TEXT AS foo", (u'�����',))
 
93
curs.execute(u"SELECT %s::TEXT AS foo", (u'àèìòù',))
91
94
x = curs.fetchone()[0]
92
95
print "  ->", x.encode('utf-8'), ":", type(x)
93
96
 
94
97
print "Setting backend encoding to UTF8 and executing queries:"
95
98
conn.set_client_encoding('UNICODE')
96
99
curs = conn.cursor()
97
 
curs.execute(u"SELECT %s::TEXT AS foo", (u'�����'.encode('utf-8'),))
 
100
curs.execute(u"SELECT %s::TEXT AS foo", (u'àèìòù'.encode('utf-8'),))
98
101
x = curs.fetchone()[0]
99
102
print "  ->", x.encode('utf-8'), ":", type(x)
100
 
curs.execute(u"SELECT %s::TEXT AS foo", (u'�����',))
 
103
curs.execute(u"SELECT %s::TEXT AS foo", (u'àèìòù',))
101
104
x = curs.fetchone()[0]
102
105
print "  ->", x.encode('utf-8'), ":", type(x)