python-cx_Oracle/samples/ReturnUnicode.py
Anthony Tuininga 340dcb7195 Rework test suite and samples so that they are independent of each other and
so that the SQL scripts used to create/drop schemas are easily adjusted to
use different schema names, if desired. Improve documentation for test suite
and samples.
2017-07-14 16:50:41 -06:00

34 lines
1.3 KiB
Python

#------------------------------------------------------------------------------
# Copyright 2016, 2017, Oracle and/or its affiliates. All rights reserved.
#
# Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved.
#
# Portions Copyright 2001-2007, Computronix (Canada) Ltd., Edmonton, Alberta,
# Canada. All rights reserved.
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# ReturnUnicode.py
# Returns all strings as unicode. This also demonstrates the use of an output
# type handler to change the way in which data is returned from a cursor.
#
# This script requires cx_Oracle 5.0 and higher.
#------------------------------------------------------------------------------
from __future__ import print_function
import cx_Oracle
import SampleEnv
def OutputTypeHandler(cursor, name, defaultType, size, precision, scale):
if defaultType in (cx_Oracle.STRING, cx_Oracle.FIXED_CHAR):
return cursor.var(unicode, size, cursor.arraysize)
connection = cx_Oracle.Connection(SampleEnv.MAIN_CONNECT_STRING)
connection.outputtypehandler = OutputTypeHandler
cursor = connection.cursor()
cursor.execute("select * from TestStrings")
for row in cursor:
print("Row:", row)