471,354 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

OCBC connection

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
Sep 28 '07 #1
4 1620
On Sep 28, 9:57 am, "Sugrue, Sean" <Sean.Sug...@analog.comwrote:
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

Sep 28 '07 #2
ky******@gmail.com wrote:
On Sep 28, 9:57 am, "Sugrue, Sean" <Sean.Sug...@analog.comwrote:
>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

Sep 28 '07 #3
On Sep 28, 1:07 pm, Steve Holden <st...@holdenweb.comwrote:
kyoso...@gmail.com wrote:
On Sep 28, 9:57 am, "Sugrue, Sean" <Sean.Sug...@analog.comwrote:
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

Sep 28 '07 #4
ky******@gmail.com wrote:
[...]
>>
> >>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

Sep 28 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by G-Fit | last post: by
11 posts views Thread by pradeep_TP | last post: by
6 posts views Thread by Chris Szabo | last post: by
18 posts views Thread by Rob Nicholson | last post: by
35 posts views Thread by Eric Sabine | last post: by
3 posts views Thread by Martin B | last post: by
20 posts views Thread by fniles | last post: by
reply views Thread by Sugrue, Sean | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.