Category: Utilities
Author/Contact Info Aaron Schlesinger aaron@mfxpop.com
Description: A Nice little script that'll let you grab data from a local database. Easy to modify.
#!/usr/bin/perl
$BASE_DIR="BASE_DIR";
$CGI_DIR="$BASE_DIR/cgi-bin";
print "This program will allow you to pull information with 1 select q
+uery from our databases\n\n";
print "What do you want? (seperate each item with a comma)  ";
$what=<STDIN>;

print "Where do you want it from? (select a database)\n";
$whereDB=<STDIN>;

print "What Table do you want it from? (select a table)\n";
$whereT=<STDIN>;

print "Any special options? (like orderby, etc?)\n";
$options=<STDIN>;

chop($file);
chop($whereT);
chop($whereDB);
chop($what);
chop($options);

$sql="SELECT $what from $whereT $options";

print "What file name?  ";
$file=<STDIN>;

`clear`;

print "\nI am going to do this:\n $sql\n";
print "I am doing it to this: $whereDB\n\n";

print "If you do not want to do it, type N:  ";
$doit=<STDIN>;
$driver="dbi:mysql:$whereDB";
$user="USER";
$password="password";

if ($doit eq "N"){die "You didnt want to do it!";}

open(A,">$file");
use DBI;
$dbh = DBI ->connect("$driver","$user","$password");
$sth = $dbh->prepare($sql);
$sth->execute || die "Unable to connect to $whereDB Database!";
while(@row=$sth->fetchrow_array)
 {
  $a=$#row;
  $a++;
  $cnt=0;
  $strng="";
  while($cnt<$a)
   {
    $strng=$strng."\"$row[$cnt]\",";
    $cnt++;
   }
  chop($strng);
  print A "$strng\n";
 }
$dbh->disconnect;

Edit: Petruchio Tue Dec 18 21:59:53 UTC 2001 - Added CODE tags.

Replies are listed 'Best First'.
Re: Get From DB
by gmax (Abbot) on Dec 19, 2001 at 14:11 UTC
    A few points:
    - Everybody in this monastery should tell you that you should use strict and warnings.
    - If that "$CGI-DIR" means anything serious, you should also have Taint checking. If it doesn't (as I strongly suspect and "-w" could have told you), what is it doing there?
    - Using DBI, you should either checking for an error after "connect", or set {RaiseError => 1}.
    - data in a datbase is seldom reduced to a single table. And even if it is, getting data from a table without a "WHERE" clause, can fetch you a million records for no sensible reason.
    - I fail to see the logic behind assigning $a =$#row and then incrementing it. How about
    while (my @row=fetchrow_array()) { print join ",", @row; print "\n"; }
    - what is the point in clearing the screen, if you are sending the output to a file?
    - "seperate" should be spelled "separate"
    - you are assuming that the user knows SQL, since (s)he is supposed to enter special options. What is the purpose of using this "utility" if I can do the same with an one-liner?
    $ mysql -t -u user -p -e "SELECT blah,Blablah from my_table" I could continue, but I think it's enough.
    gmax
      Or for that matter, I could use these commands in Emacs
      M-x sql-oracle

      OR
      M-x sql-mysql

      to connect to any database and do any number of SQL queries my soul desires. Also, the advantage of using Emacs (or XEmacs! ;) is that you have a buffer where all your SQL may be saved to and retreaved for later use. Editing is also a snap.


      "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith
Re: Get From DB
by princepawn (Parson) on Dec 19, 2001 at 03:13 UTC
    I've been reviewing the multitude of CPAN offerings for such things...

    So far, the most comprehensive and efficient solution is PApp::SQL.

Re: Get From DB (with dbish instead)
by grinder (Bishop) on Dec 21, 2001 at 15:17 UTC

    Did you know that the DBI distribution comes bundled with dbish, a shellish interface to DBI? It can do all sorts of nifty things. You'll find it more powerful, and if you find that it lacks something, patching it to make it do more and then sending those back to the author will guarantee you enduring fame, wealth and power.

    Well I might be lying about that last part.

    --
    g r i n d e r
    just another bofh

    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u';