Re: checking column types from cursor object in a database-independent way?

"Vernon D. Cole" <[email protected]>
Newsgroups gmane.comp.python.db
Message-ID <CAH-ZgAfva_+uk-BOO8U3bvQTsSDXkFEJxdX+w9Xr2FermDvg5g@mail.gmail.com>
Give the restriction you mention (that you have only the cursor object) the
solution you propose looks pretty good.

The  cursor.connection attribute is an extension, but I think most api
modules provide it.


On Fri, Apr 19, 2013 at 11:56 AM, Dan Lenski <[email protected]> wrote:

> Hi,
> I have to dump some data from various databases (Oracle and Postgre, at the
> moment) into CSV files, while preserving the column type information so the
> data can be correctly loaded into another application.  Basically, I need
> to
> write a function that can distinguish string/numeric data in the columnar
> data
> from a cursor object in a fashion that's independent of the particular
> DBAPI
> module in use.
>
> According to PEP-0249, each DBAPI module has to define type objects (e.g.
> module.STRING) which "must compare equal to" the type objects in the cursor
> object for each column.
>
> The problem I'm having is... how can I figure out what are the appropriate
> type objects for this comparison if I *only* have access to the cursor
> object?
> I've been using an ugly kludge so far, to get a handle on the right DBAPI
> module:
>
>     mod = sys.modules[cur.connection.__class__.__module__] #FIXME!
>     if cur.description[0][1] == mod.STRING:
>         print "first column is STRING data type"
>
> Is there a more robust and elegant solution to this?  It seems like there
> ought to be a module-independent way to introspect the column data types in
> the cursor object.
>
> Thanks,
> Dan Lenski
>
> _______________________________________________
> DB-SIG maillist  -  [email protected]
> http://mail.python.org/mailman/listinfo/db-sig
>

_______________________________________________
DB-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/db-sig
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.