-
Notifications
You must be signed in to change notification settings - Fork 358
/
return_numbers_as_decimals.py
31 lines (26 loc) · 1.33 KB
/
return_numbers_as_decimals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#------------------------------------------------------------------------------
# Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# return_numbers_as_decimals.py
# Returns all numbers as decimals by means of an output type handler. This is
# needed if the full decimal precision of Oracle numbers is required by the
# application. See this article
# (http://blog.reverberate.org/2016/02/06/floating-point-demystified-part2.html)
# for an explanation of why decimal numbers (like Oracle numbers) cannot be
# represented exactly by floating point numbers.
#
# This script requires cx_Oracle 5.0 and higher.
#------------------------------------------------------------------------------
import decimal
import cx_Oracle as oracledb
import sample_env
def output_type_handler(cursor, name, default_type, size, precision, scale):
if default_type == oracledb.NUMBER:
return cursor.var(decimal.Decimal, arraysize=cursor.arraysize)
connection = oracledb.connect(sample_env.get_main_connect_string())
connection.outputtypehandler = output_type_handler
cursor = connection.cursor()
cursor.execute("select * from TestNumbers")
for row in cursor:
print("Row:", row)