Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can somebody help me out with using DBD module..?

I need to accept an SQL query from the command line, then the DBD has to take care of writing it to or reading from a textfile database..similarly for other commands like create table, select, Update , drop etc... How do I do it ? Can somebody ? Plz

diparun

Replies are listed 'Best First'.
Re: using DBD
by jZed (Prior) on Apr 21, 2004 at 23:48 UTC

    DBD::AnyData or DBD::CSV will take care of creating textfiles if that's what you really need. You should consider DBD::SQLite or a full RDBMS if you don't need textfiles.

    In terms of the command line, you can use dbish, the DBI Interactive Shell found in DBI::Shell or for simpler needs, you can just write a very tiny wrapper that prepares and executes SQL from the command line along these lines (careful, this works, if you create or drop tables with it, you'll be creating or droping files):

    #!perl -w use strict; use DBI; sub do_help{ print " Squish, the SQL Undernourished Interactve Shell q | quit Quit the program h | help | ? view this Help text Anything else you enter is passed as a SQL string to DBD::CSV. The SQL will be prepared and executed. squish.pl, copyright 2004 by Jeff Zucker all rights reserved may be freely modified or distributed under the same terms as perl ";} my $dbh=DBI->connect('DBI:CSV(RaiseError=1,PrintError=0):'); print "Welcome to squish, the SQL Undernourished Interactve Shell!\n"; while (1) { print "squish> "; my $input = <>; chomp $input; next unless $input; for ($input) { /^(q|quit)$/i &&do{do_exit()}; /^(h|\?|help)$/i &&do{do_help(); last}; my($sth,$rv); eval { $sth = $dbh->prepare( $input ); $rv = $sth->execute; }; print $@ if $@; /^SELECT/i &&do { $sth->dump_results; last}; print "$rv\n"; } } sub do_exit { $dbh->disconnect; exit; }

Re: using DBD
by Happy-the-monk (Canon) on Apr 21, 2004 at 22:20 UTC
Re: using DBD
by Zaxo (Archbishop) on Apr 21, 2004 at 23:09 UTC

    For the most part, you don't need to deal with DBD::* directly, not even to use it. The DBI module figures out which DBD driver to load from the "connect string" given the constructor, &DBI::connect. You rarely need to call driver subs directly, just call DBI methods and let DBI sort it out.

    Usually, all you need to do about particular drivers is check whether all the DBI methods you want to use are supported.

    After Compline,
    Zaxo

Re: using DBD
by borisz (Canon) on Apr 21, 2004 at 22:27 UTC
    Not enough information. But most databases have already a tool of this kind. sqlite for sqlite or psql for portgres.
    Boris