summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorFederico Di Gregorio <fog@initd.org>2005-11-16 17:30:45 +0000
committerFederico Di Gregorio <fog@initd.org>2005-11-16 17:30:45 +0000
commit996bd07c85e2ea35d6b6ca6bf925ae87b62e74d1 (patch)
tree9295193b28182c7dc79065641303f82e64a04c8e /examples
parent000aa345ac984d9a570a679a58147759a4db7ac1 (diff)
downloadpsycopg2-996bd07c85e2ea35d6b6ca6bf925ae87b62e74d1.tar.gz
Definitely fixed date and time adapting problems (for mx too!)
Diffstat (limited to 'examples')
-rw-r--r--examples/dt.py8
-rw-r--r--examples/encoding.py32
-rw-r--r--examples/tz.py2
3 files changed, 24 insertions, 18 deletions
diff --git a/examples/dt.py b/examples/dt.py
index 9933164..76f0c78 100644
--- a/examples/dt.py
+++ b/examples/dt.py
@@ -23,6 +23,8 @@ import psycopg2
import mx.DateTime
import datetime
+from psycopg2.extensions import adapt
+
if len(sys.argv) > 1:
DSN = sys.argv[1]
@@ -73,9 +75,11 @@ for n, x in zip(mx1[1:], curs.fetchone()):
try:
# this will work only is psycopg has been compiled with datetime
# as the default typecaster for date/time values
- s = repr(n) + "\n -> " + repr(x) + "\n -> " + x.isoformat()
+ s = repr(n) + "\n -> " + str(adapt(n)) + \
+ "\n -> " + repr(x) + "\n -> " + x.isoformat()
except:
- s = repr(n) + "\n -> " + repr(x) + "\n -> " + str(x)
+ s = repr(n) + "\n -> " + str(adapt(n)) + \
+ "\n -> " + repr(x) + "\n -> " + str(x)
print s
print
diff --git a/examples/encoding.py b/examples/encoding.py
index 94af0d0..da57bcf 100644
--- a/examples/encoding.py
+++ b/examples/encoding.py
@@ -1,5 +1,5 @@
-# encoding.py - how to change client encoding (and test it works)
-# -*- encoding: latin-1 -*-
+# enkoding.py - show to change client enkoding (and test it works)
+# -*- encoding: utf8 -*-
#
# Copyright (C) 2004 Federico Di Gregorio <fog@debian.org>
#
@@ -33,27 +33,29 @@ print "Initial encoding for this connection is", conn.encoding
print "\n** This example is supposed to be run in a UNICODE terminal! **\n"
print "Available encodings:"
-for a, b in psycopg2.extensions.encodings.items():
+encs = psycopg2.extensions.encodings.items()
+encs.sort()
+for a, b in encs:
print " ", a, "<->", b
print "Using STRING typecaster"
print "Setting backend encoding to LATIN1 and executing queries:"
conn.set_client_encoding('LATIN1')
curs = conn.cursor()
-curs.execute("SELECT %s::TEXT AS foo", ('�����',))
+curs.execute("SELECT %s::TEXT AS foo", ('àèìòù',))
x = curs.fetchone()[0]
print " ->", unicode(x, 'latin-1').encode('utf-8'), type(x)
-curs.execute("SELECT %s::TEXT AS foo", (u'�����',))
+curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
x = curs.fetchone()[0]
print " ->", unicode(x, 'latin-1').encode('utf-8'), type(x)
print "Setting backend encoding to UTF8 and executing queries:"
conn.set_client_encoding('UNICODE')
curs = conn.cursor()
-curs.execute("SELECT %s::TEXT AS foo", (u'�����'.encode('utf-8'),))
+curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù'.encode('utf-8'),))
x = curs.fetchone()[0]
print " ->", x, type(x)
-curs.execute("SELECT %s::TEXT AS foo", (u'�����',))
+curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
x = curs.fetchone()[0]
print " ->", x, type(x)
@@ -63,20 +65,20 @@ psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
print "Setting backend encoding to LATIN1 and executing queries:"
conn.set_client_encoding('LATIN1')
curs = conn.cursor()
-curs.execute("SELECT %s::TEXT AS foo", ('�����',))
+curs.execute("SELECT %s::TEXT AS foo", ('àèìòù',))
x = curs.fetchone()[0]
print " ->", x.encode('utf-8'), ":", type(x)
-curs.execute("SELECT %s::TEXT AS foo", (u'�����',))
+curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
x = curs.fetchone()[0]
print " ->", x.encode('utf-8'), ":", type(x)
print "Setting backend encoding to UTF8 and executing queries:"
conn.set_client_encoding('UNICODE')
curs = conn.cursor()
-curs.execute("SELECT %s::TEXT AS foo", (u'�����'.encode('utf-8'),))
+curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù'.encode('utf-8'),))
x = curs.fetchone()[0]
print " ->", x.encode('utf-8'), ":", type(x)
-curs.execute("SELECT %s::TEXT AS foo", (u'�����',))
+curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
x = curs.fetchone()[0]
print " ->", x.encode('utf-8'), ":", type(x)
@@ -85,19 +87,19 @@ print "Executing full UNICODE queries"
print "Setting backend encoding to LATIN1 and executing queries:"
conn.set_client_encoding('LATIN1')
curs = conn.cursor()
-curs.execute(u"SELECT %s::TEXT AS foo", ('�����',))
+curs.execute(u"SELECT %s::TEXT AS foo", ('àèìòù',))
x = curs.fetchone()[0]
print " ->", x.encode('utf-8'), ":", type(x)
-curs.execute(u"SELECT %s::TEXT AS foo", (u'�����',))
+curs.execute(u"SELECT %s::TEXT AS foo", (u'àèìòù',))
x = curs.fetchone()[0]
print " ->", x.encode('utf-8'), ":", type(x)
print "Setting backend encoding to UTF8 and executing queries:"
conn.set_client_encoding('UNICODE')
curs = conn.cursor()
-curs.execute(u"SELECT %s::TEXT AS foo", (u'�����'.encode('utf-8'),))
+curs.execute(u"SELECT %s::TEXT AS foo", (u'àèìòù'.encode('utf-8'),))
x = curs.fetchone()[0]
print " ->", x.encode('utf-8'), ":", type(x)
-curs.execute(u"SELECT %s::TEXT AS foo", (u'�����',))
+curs.execute(u"SELECT %s::TEXT AS foo", (u'àèìòù',))
x = curs.fetchone()[0]
print " ->", x.encode('utf-8'), ":", type(x)
diff --git a/examples/tz.py b/examples/tz.py
index 8fe40af..c27bf30 100644
--- a/examples/tz.py
+++ b/examples/tz.py
@@ -1,5 +1,5 @@
# tz.py - example of datetime objects with time zones
-# -*- encoding: latin1 -*-
+# -*- encoding: utf8 -*-
#
# Copyright (C) 2004 Federico Di Gregorio <fog@debian.org>
#