Re: SQL access without Dataminer (perl off topic) [message #34011] |
Mon, 10 February 2003 17:59 |
dmarino
Messages: 9 Registered: November 2002
|
Junior Member |
|
|
> Any takers?
Here's how I'd do this with Perl, very briefly.
The hardest part is to install the needed perl modules. All available
at www.cpan.org. You'll need the DBI module and at least one DBD
(database driver)
module. This example will use PostgreSQL. Your sys admin may hook
you up. Of course you'd do lots of error checking and whatnot,
but here's the gist
Write a perl file like this, turn on the execute bit:
_________________
#! /my/path/to/perl ...
# sql.pl
use DBI; # perl databse interface
use DBD::Pg; # driver for postgres interface
# db connection info
$dbuser='dbuser';$dbpasswd='dbpasswd';$dbhost='dbhost';$dbna me='my_database';
# connect to db
$dbh=DBI->connect("dbi:Pg:dbname=$dbname,dbhost=$dbhost;", "$dbuser","$dbpasswd");
#get your query
$statement=$ARGV[0]; # you passed it in as the first arg
#execute your query
$sth=$dbh->prepare($statement);
$sth->execute();
$sth->finish();
$dbh->disconnect;
#print the results to stdout and spawn will pick em up
# this is giving you a row at a time, could do other ways
$i=0;
while(@rows=$sth->fetchrow_array()){
print("$rows[$i]");
$i++;
}
# end perl
________________________
Then you can do something as simple as this in IDL
_______________
pro getdata
; call perl to query db
myquery="SELECT somedata FROM mytable WHERE myconditions"
cmd=strjoin(['perl sql.pl ',myquery],'');
spawn, cmd, results
;do something with the results
for i=0,((size(results,/n_elements))-1) do begin
print, results[i]
endfor
end ;pro getdata
___________________
You'd want to write a lot more support code of course on both ends,
but this
is quite a useful way to get some data quick and dirty (hey whatever
works, right?). No cost, easy to port around OS's, very easy to do for
IDL-heads I'd say for sure :-)
Good luck!
D Marino
|
|
|