#!/usr/bin/perl use warnings; use strict; use DBI; use Term::ReadLine; use Switch; my $dbh = DBI->connect("dbi:Sybase:server=172.23.1.12;database=XXXXX","YYYYY","ZZZZZZ") or die "$DBI::errstr\n"; my $term = new Term::ReadLine 'Simple DBI Shell'; my $prompt = "Enter your query: "; my $OUT = $term->OUT || \*STDOUT; print $OUT "Supports select statements only - type quit to exit cleanly!\n\n"; while ( defined ($_ = $term->readline($prompt)) ) { my $res = &doit($_); print $OUT $res, "\n" unless $@; $term->addhistory($_) if /\S/; } sub doit{ my $q = shift; $q =~ /^(\w+)/; switch(lc($1)){ case "select" {return &select_case($q)}; case "quit" {&byebye} else {return "ERROR: Only select statement and \'quit\' are allowed at this time"} } } # processes select statements sub select_case { my $q = shift; my $sth = $dbh->prepare($q); my $rv = $sth->execute(); #print put column names foreach (@{$sth->{NAME}}){ print $_."\t"; } print "\n"; #print out column values while (my @row = $sth->fetchrow_array){ foreach my $val (@row){ if (!defined($val)){ $val = 'NULL'; } print $val."\t"; } print "\n"; } $sth->finish(); } sub byebye { print $OUT "Have a nice day!\n"; $dbh->disconnect(); }