OCBC connection
Steve Holden
steve at holdenweb.com
Fri Sep 28 14:07:05 EDT 2007
kyosohma 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
More information about the Python-list
mailing list