Results 1 to 3 of 3

Thread: Perl / Database

  1. #1
    Join Date
    Aug 2004
    Posts
    5

    Question Unanswered: Perl / Database

    Hi
    Can you please tell me how to write a command line tool to read the name of a table and the name of a column in that table from the user. Then to print out the values in that column to the user. Note that $myvar = <STDIN>; reads a single line of text from the user.What built in classes should be used and what would they do?. it doesnt matter if all the perl syntax is not remembered, as i just need a general idea of how to do it.

    This is what i have so far, but it still does not work. Could anyone help me and tell me where i am going wrong?

    Thanks

    ----------------------------------------------------------------------
    #!/usr/local/bin/perl

    use DBI;
    use strict;
    use Diagnostics;

    if (! $data )
    {
    @letters = getLetters();
    }

    sub getLetters
    {
    $db = DBI->connect( "DBI:CSV:f_dir=data" );

    if (! $db )
    {
    die print "Cannot Connect to Database";
    }

    my $stat = $db->prepare(
    "SELECT DISTINCT letter FROM Dictionary ORDER BY letter ASC" );
    $stat->execute();

    while( my @row = $stat->fetchrow_array )
    {
    push( @letters, $row[0] );
    }

    $db->disconnect();

    @letters;
    }
    --------------------------------------------------------------------

  2. #2
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Code:
    #! /usr/bin/perl
    
    use strict;
    use warnings;
    use DBI;
    
    print "Table: ";
    my $table = <STDIN>;
    chomp $table;
    
    print "Column: ";
    my $col = <STDIN>;
    chomp $col;
    
    my $dbh = DBI->connect("DBI:CSV:f_dir=data" );
    my $sth = $dbh->prepare("SELECT $col FROM $table;");
    $sth->execute();
    while(my ($result) = $sth->fetchrow_array()){
      print $result,"\n";
    }
    $dbh->disconnect();
    board.perl-community.de - The German Perl-Community

  3. #3
    Join Date
    Aug 2004
    Posts
    5

    Hi

    You Are A Gangster! Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •