OCBC connection

kyosohma at gmail.com kyosohma at gmail.com
Fri Sep 28 15:46:09 EDT 2007


On Sep 28, 1:07 pm, Steve Holden <st... at holdenweb.com> wrote:
> kyoso... at gmail.com wrote:
> > On Sep 28, 9:57 am, "Sugrue, Sean" <Sean.Sug... at analog.com> wrote:
> >> I'm trying to make an odbc connection to postgresql which is on a server
> >> using python.
> >> Does anyone have a code snippet to make a basic connection with a select
> >> query?
>
> >> Sean
>
> > Sean,
>
> > This appears to be what you're looking for:
>
> >http://www.devx.com/opensource/Article/29071
>
> > See also Python Database spec and module page:
>
> >http://www.python.org/topics/database/
>
> Mike:
>
> This doesn't address the ODBC part of the inquiry. I was actually going
> to respond saying I wasn't aware of an ODBC driver for PostgreSQL
> (though I'd be surprised if there wasn't one).
>
> Using the psycopg2 module, which is my preferred PostgreSQL interface
> module, it's easy to answer:
>
>  >>> curs = conn.cursor()
>  >>> import psycopg2 as db
>  >>> conn = db.connect(database="pycon", user="username",
>          password="password", host="localhost", port=5432)
>  >>> curs = conn.cursor()
>  >>> curs.execute("SELECT orgid, orgname FROM organization")
>  >>> from pprint import pprint # just for neatness
>  >>> pprint(curs.fetchall())
> [(1, 'AB Strakt'),
>   (79, 'DevIS'),
>      ...
>   (113, 'Test Organization'),
>   (19, 'Holden Web LLC')]
>  >>>
>
> regards
>   Steve
> --
> Steve Holden        +1 571 484 6266   +1 800 494 3119
> Holden Web LLC/Ltd          http://www.holdenweb.com
> Skype: holdenweb      http://del.icio.us/steve.holden
>
> Sorry, the dog ate my .sigline

Whoops! I almost posted some code using the adodb or the odbc module,
which is what I would have probably used, but I figured I'd take a
look and see if there was a postgres module available. I'm not seeing
any basic differences between your code and the code I linked to
though...except that if I ran your first line of code, I would get an
exception as "conn" hasn't been defined yet.

Whatever. I apologize for being misleading(?)

Mike




More information about the Python-list mailing list