2006-02-16

Simple perl script to query sn database

Very generic perl script, lets name getsn.pl :)

#!/usr/bin/perl

use strict;
use DBI;

my $DSN = 'DBI:Pg:dbname=cas;port=5432';

my $dbh = DBI->connect($DSN);
die "Couldn't connect to database \'$DSN\' $!\n" if ( ! $dbh );

my $sql = "select pgc,objname, id from ledasn_ell order by id limit 10;";

my $sth=$dbh->prepare($sql) || die "Couldn't prepare \'$sql\' $!\n";

if ( $sth->execute() ) {
  while ( defined ( my $r=$sth->fetchrow_hashref ) ) {
    foreach my $key (keys %$r ) {
      print "$key => $r->{$key} ";
    }
    print "\n";
 }
}

$sth->finish if $sth;

Now run it

$ perl getsn.pl
objname => NGC0224 id => 1885.A pgc => 2557
objname => NGC4424 id => 1895.A pgc => 40809
objname => NGC5253 id => 1895.B pgc => 48334
objname => NGC4674 id => 1907.A pgc => 43050
objname => NGC2841 id => 1912.A pgc => 26512
objname => NGC4527 id => 1915.A pgc => 41789
objname => NGC4486 id => 1919.A pgc => 41361
objname => NGC2608 id => 1920.A pgc => 24111
objname => NGC4303 id => 1926.A pgc => 40001
objname => NGC6181 id => 1926.B pgc => 58470

Improve it a bit, now accept argument supernovae id.

#!/usr/bin/perl

use strict;
use DBI;

my $sn = $ARGV[0];
$sn .= '.*';

my $DSN = 'DBI:Pg:dbname=cas;port=5432';

my $dbh = DBI->connect($DSN);
die "Couldn't connect to database \'$DSN\' $!\n" if ( ! $dbh );

my $sql = "select pgc,objname, id from ledasn_ell where id ~ ? order by id limit 10;";

my $sth=$dbh->prepare($sql) || die "Couldn't prepare \'$sql\' $!\n";

if ( $sth->execute($sn) ) {
  while ( defined ( my $r=$sth->fetchrow_hashref ) ) {
    foreach my $key (keys %$r ) {
      print "$key => $r->{$key} ";
    }
    print "\n";
 }
}
$sth->finish if $sth;
$ perl getsn2.pl 1972.
objname => PGC1813755 id => 1972.A pgc => 1813755
objname => NGC3947 id => 1972.C pgc => 37264
objname => NGC5253 id => 1972.E pgc => 48334
objname => PGC038449 id => 1972.F pgc => 38449
objname => NGC3147 id => 1972.H pgc => 30019
objname => NGC7634 id => 1972.J pgc => 71192
objname => NGC0735 id => 1972.L pgc => 7282
objname => NGC7564 id => 1972.M pgc => 70843
objname => UGC00279 id => 1972.N pgc => 1736
objname => NGC2841 id => 1972.R pgc => 26512