Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: database

Reply
Thread Tools

RE: database

 
 
Pettersen, Bjorn S
Guest
Posts: n/a
 
      09-18-2003
> From: Alberto Vera [private.php?do=newpm&u=]
>
> Hello:
>
> Do you have any example about how access to a database
> using ODBC or native driver?


using the win32all odbc module

>>> import odbc
>>> cn = odbc.odbc('AcctDB')
>>> c = cn.cursor()
>>> c.execute('select * from AcctMetaInfo order by snapshotdate')

0
>>> row = c.fetchone()[:10] # first ten columns
>>> row

(141410449310712784L, 7051, <DbiDate object at 0x0089E070>, 2147483646,
0, 2147483646, '?1', 2147483646, '?1', <DbiRaw object at 0x0089E080>)
>>> str(row[2])

'Mon Apr 15 11:14:04 2002'
>>> str(row[-1])

'\t\x13\x14\x01\x00\x00\x00\x00\x00\x00\x00'
>>>



using the adodbapi module (http://sourceforge.net/projects/adodbapi):

>>> import adodbapi
>>> cn = adodbapi.connect('AcctDB')
>>> c = cn.cursor()
>>> c.execute('select * from AcctMetaInfo order by snapshotdate')
>>> row = c.fetchone()[:10]
>>> row

(141410449310712779L, 7051, datetime.datetime(2002, 4, 15, 11, 14, 4),
2147483646, 0, 2147483646, u'?1', 2147483646, u'?1', <read-write buffer
ptr 0x00A53C94, size 11 at 0x00A53C78>)
>>> row[2]

datetime.datetime(2002, 4, 15, 11, 14, 4)
>>> str(row[-1])

'\t\x13\x14\x01\x00\x00\x00\x00\x00\x00\x00'

using query analyzer:

AccountID ...ID SnapshotDate ...
-------------------- ----------- ------------------------
141410449310712779 7051 2002-04-15 11:14:04.000 ...

The account id is a bigint, and as you can see the odbc module loses
precision because it is first converted to a float internally.

Both modules convert currency/money data to float:

>>> c.execute('select Amount from FinancialInfo where acountid = 1234')
>>> c.fetchone()

(80.439999999999998,)
>>>


which is of course completely horrible.

With the adodbapi module you can change the default behavior however:

----------------------
import adodbapi

class Money(long):
def __new__(self, value):
return long.__new__(Money, value)

def __repr__(self):
d, c = divmod(self, 10000)
return '$%s.%s' % (d,c)

def cvt_money((hi,lo)):
# not extensively tested
return Money((hi<<32) + (lo < 0 and (2**32+lo) or lo))

adodbapi.variantConversions[adodbapi.adCurrency] = cvt_money

cn = adodbapi.connect('AcctDb')
c = cn.cursor()

c.execute('select Amount from FinancialInfo where where acountid =
1234')
print c.fetchone()
----------------------

the output:

($80.4400,)

which is much better

I don't have mxODBC, nor any native drivers to test with (although I'd
be interested in the results if anyone else does..)

taking-a-break-from-mdx'ly y'rs,
-- bjorn

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
exporting an excel file from database; making changes to excel file and updating the database by importing it back Luis Esteban Valencia ASP .Net 1 01-12-2005 12:28 AM
Re: Software Developer to transcribe Oracle database into Access database Fluker MCSD 0 07-09-2003 01:20 PM
Re: Software Developer to transcribe Oracle database into Access database Guy Cox MCSD 1 07-09-2003 08:07 AM



Advertisments
 



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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57