OCBC connection

S

Sugrue, Sean

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
 
S

Steve Holden

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:
[(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
 
M

M.-A. Lemburg

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).

There is one:

http://www.postgresql.org/ftp/odbc/versions/

and it works quite well with mxODBC on all supported platforms.

Here's a step-by-step guide for the Windows installation:

http://www.sparxsystems.com/EAUserGuide/setupapostgresqlodbcdriver.htm

Note that you should add these settings to your odbc.ini
section for the PostgreSQL data source in order to avoid
problems with BLOBs:

# Fetch BYTEA and TEXT as LongVarXXX column types:
ByteaAsLongVarBinary = 1
TextAsLongVarchar = 1

Alternatively, you can try the OpenLink driver:

http://uda.openlinksw.com/odbc/st/odbc-postgres-st/

If you're on Max OS, you should have a look at the ActualTech
drivers:

http://www.actualtechnologies.com/product_opensourcedatabases.php

Regards,
--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Sep 28 2007)________________________________________________________________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::


eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
 
K

kyosohma

This appears to be what you're looking for:

See also Python Database spec and module page:

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:
[(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
 
S

Steve Holden

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')]
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(?)
I screwed up anyway - the first line shouldn't have been included.

But perhaps Sean could enlighten us as to whether ODBC is really a
requirement, or simply the first solution that he thought of. I don't
know why one would *require* ODBC.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top