views:

560

answers:

2

I have a perl script that interfaces with an existing database (type of database is unknown) through the DBI module, that I would like to access in python 2.6 on winXP.

The perl code is:

use DBI;
my $DSN = "DBI:Proxy:hostname=some.dot.com;port=12345;dsn=DBI:XXXX:ZZZZZ";
my $dbh = DBI->connect($DSN);

Can this be translated into a python equivalent?

Following an example at (http://stackoverflow.com/questions/768250/is-there-any-pywin32-odbc-connector-documentation-available/768352#768352 ) I've put together the following:

import odbc
DSN = "DBI:Proxy:hostname=some.dot.com;port=12345;dsn=DBI:XXXX:ZZZZZ"
db = odbc.odbc(DSN)

But I get the error: dbi.operation-error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified in LOGIN

UPDATE

It appears that another perl module, DBD::Proxy is providing the actual interface to a Perl DBI::ProxyServer (server-side) implementation that handles the actual queries.

Can python be used to interface with the Perl-based DBI::ProxyServer?

http://search.cpan.org/~timb/DBI-1.608/lib/DBD/Proxy.pm http://hell.org.ua/Docs/oreilly/weblinux/dbi/ch08_02.htm [Thanks araqnid]

+5  A: 

Your python script doesn't have to be a line by line translation of your Perl script.

Why not just use the Python DB-API compatible module for the database you want to access? For MySQL, use MySQLdb. For PostgreSQL, use PyGreSQL.

Or search Google for "YourDatabaseName + python"

Seun Osewa
-1: "doesn't have to be a line-by-line translation" should be "can't be a line-by-line translation".
S.Lott
What if I don't know the backend database? (I suppose I could try different modules). Does the Perl DBI module magically figure out the backend db from the given $DSN value?
monkut
A database can never be consulted entirely generically... ports, connection protocols, sql syntax... all slightly different. The Python DB-API provides a common interface that each database's driver should implement so that the basics are as consistent as possible. Perl's DBI is the same... a layer of indirection designed to isolate each DB's (and DB-driver's) libraries.
Jarret Hardie
Yes, perl DBI automagically loads DBD::xx when asked to connect to a URL starting "dbi:xx:", so in this case DBD::Proxy.I guess the underlying problem is that DBD::Proxy isn't a real database driver, but a connector to talk to a bridge on some.dot.com whose dsn is (hopefully) a real database driver. Which is likely to use a DBD::Proxy-specific protocol.
araqnid
araqnid, ok this is making more sense now. That sounds like what it's doing from what I can see. And would explain my DBD::Proxy error I'm getting in the perl version at the moment. Now is there a way to duplicate the DBD::Proxy functionality in python?
monkut
It should be possible, but a quick glance at DBD/Proxy.pm suggests it will be a *lot* of work. For instance, you'll need to figure out how the protocol for RPC::PlClient works. That itself uses Storable to marshal Perl objects, and that's a nontrivial transformation to Python: depends on how complicated the DBI-related objects are.
araqnid
Yeah, it's looking difficult, I'll probably just end up using the perl.
monkut
A: 

sqlalchemy is pretty nice.

blackkettle
Can sqlalchemy interface with a Perl DBI::ProxyServer?
monkut
Why on earth would you want a Python DB framework to integrate with a perl DBI construct, especially given that there are perfectly functional database interfaces in Python?
Jarret Hardie
sorry i clearly misunderstood the question.
blackkettle
or perhaps i just answered before that last line was added to the update.
blackkettle