Wrong transfer of data (ODBC)

Santiago it.santiago at memo.volvo.se
Fri Mar 15 13:12:27 EST 2002


I was trying to use the 'Python ODBC Notes (Windows Only)'  wrote by John
Dell'Aguila 2/11/98.(at the bottom you will see the program)
I used the ODBC using Python Database API. from the AS/400 database to the
PC Windows.

But when I get the data from the AS/400 I get the string fields okay, but
the fields with decimals , like price, it appears with wrong decimals.
The price field in the AS/400 has eleven digits (9 integers and 2 decimals).

For example, the data in the AS/400 is 15860.94  but what I get is
15860.94000000001.
Column descriptions:
          ('mater', 'STRING', 20, 20, 0, 0, 0)
          ('descr', 'STRING', 18, 18, 0, 0, 0)
          ('price', 'NUMBER', 13, 11, 11, 2, 0)
          ('stock', 'NUMBER', 7, 5, 5, 0, 0)
Data
('                 9534', 'GEARBOX              ', 15860.940000000001, 0L)
('                 9632', 'GEARBOX              ', 15962.969999999999, 1L)
('                 9642', 'GEARBOX              ', 15090.389999999999, 0L)
('                 9643', 'GEARBOX O          ', 15090.389999999999, 1L)
('                 9686', 'GEARBOX  8686    ', 15090.389999999999, 1L)
('                 9827', 'GEARBOX 8827     ', 18053.43, 0L)
('                 9862', 'GEARBOX  8862    ', 15962.969999999999, 0L)
('                 9892', 'GEARBOX 8892     ', 17155.32, 0L)
('                 9908', 'GEARBOX 18         ', 18535.619999999999, 0L)
('                 9911', 'GEARBOX 8911     ', 18053.43, 0L)

Where is the problem?
David

PD: program that I used as a base:

   import dbi, odbc     # ODBC modules
   import time          # standard time module

   dbc = odbc.odbc(     # open a database connection
       'sample/monty/spam'  # 'datasource/user/password'
       )
   crsr = dbc.cursor()  # create a cursor
   crsr.execute(        # execute some SQL
       """
       SELECT country_id, name, insert_change_date
       FROM country
       ORDER BY name
       """
       )
   print 'Column descriptions:'  # show column descriptions
   for col in crsr.description:
       print ' ', col
   result = crsr.fetchall()      # fetch the results all at once
   print '\nFirst result row:\n ', result[0]  # show first result row
   print '\nDate conversions:'   # play with dbiDate object
   date = result[0][-1]
   fmt = '  %-25s%-20s'
   print fmt % ('standard string:', str(date))
   print fmt % ('seconds since epoch:', float(date))
   timeTuple = time.localtime(date)
   print fmt % ('time tuple:', timeTuple)
   print fmt % ('user defined:', time.strftime('%d %B %Y', timeTuple))
   -------------------------------output--------------------------------

   Column descriptions:
     ('country_id', 'NUMBER', 12, 10, 10, 0, 0)
     ('name', 'STRING', 45, 45, 0, 0, 0)
     ('insert_change_date', 'DATE', 19, 19, 0, 0, 1)

   First result row:
     (24L, 'ARGENTINA', <DbiDate object at 7f1c80>)

   Date conversions:
     standard string:         Fri Dec 19 01:51:53 1997
     seconds since epoch:     882517913.0
     time tuple:              (1997, 12, 19, 1, 51, 53, 4, 353, 0)
     user defined:            19 December 1997






More information about the Python-list mailing list