Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

DBI use

by Win (Novice)
on Nov 16, 2004 at 12:40 UTC ( [id://408094]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: DBI use
by mpeppler (Vicar) on Nov 16, 2004 at 12:51 UTC
    Please don't just post a bunch of code and ask us to figure out what is wrong. At the very least you should post the actual error messages that you get.

    Michael

Re: DBI use
by reneeb (Chaplain) on Nov 16, 2004 at 12:53 UTC
    First of all you should give us the error message! We have no visionary power to see what goes wrong!

    But
    my $Command = $1; # EXEC Command - defined earlier my $Request_id = $2;


    must be:
    my($Command,$Request_id) = @_;
Re: DBI use
by trammell (Priest) on Nov 16, 2004 at 14:44 UTC
    Sure.
    sub Got_Command ($$) { # bad routine name. Sounds like it should return a boolean. # icky prototypes! Should be: # sub Got_Command { my $Command = $1; # EXEC Command - defined earlier my $Request_id = $2; # bad variable names--should be lowercase # bad parameter passing--do you mean # my ($command, $request_id) = @_[0,1]; ? # Pass parameters to a stored procedure print "$Command\n"; my $sthB_A = $dbh->prepare("$Command") or die "Couldn't prepare query: ".$dbh->errstr; # useless use of quotes--prepare($command) $sthB_A->execute() or die "Couldn't execute query: ".$sthB_A->errstr; ########################################## # Return Results here # ########################################## # lame comment style--hard on the eyes # also it appears you need to "use strict" $Return_results = "Select * from Result_storage_keep where Unique_id +entifier = '".$Request_id."'\;"; # should enumerate select columns and use # SQL statement placeholders, i.e. # my $return_results = "Select col1, col2 from # Result_storage_keep where Unique_id=?"; my $get_results_B = $dbh->prepare("$Return_results") or die "Couldn +'t prepare query: ".$dbh->errstr; # more useless use of quotes, prepare($results) $get_results_B->execute() or die "Couldn't execute query: ".$get_results_B->errstr; while (my @row = $get_results_B->fetchrow_array ) { } foreach (@row){ print OUTPUT_FILE "$item0\n"; # where does $item0 come from? } }
Re: DBI use
by erix (Prior) on Nov 16, 2004 at 12:55 UTC

    Maybe I did not see not all errors. I could not run it.

    #sub Got_Command ($$) { # don't use prototypes sub Got_Command { # my $Command = $1; # EXEC Command - defined earlier # my $Request_id = $2; my ($Command,$Request_id) = @_; # Pass parameters to a stored procedure print "$Command\n"; my $sthB_A = $dbh->prepare("$Command") or die "Couldn't prepare que +ry: ".$dbh->errstr; $sthB_A->execute() or die "Couldn't execute query: ".$sthB_A->errstr +; ########################################## # Return Results here # ########################################## $Return_results = "Select * from Result_storage_keep where Unique_id +entifier = '".$Request_id."'\;"; my $get_results_B = $dbh->prepare("$Return_results") or die "Couldn +'t prepare query: ".$dbh->errstr; # $sth is conventional name for statement handle $get_results_B->execute() or die "Couldn't execute query: ".$get_res +ults_B->errstr; while (my @row = $get_results_B->fetchrow_array ) { foreach my $item (@row){ print OUTPUT_FILE "$item\n"; } } }
      Does the file handle have to be within the subroutine?

        If you want to write your database data somewhere else, take it there.

        You can gather the @rows and return the data. Or you can pass a filename/filehandle into the subroutine, which is probably better.

        But you must really get your questions more precise, and give error messages. Not everybody has as much time as I take.

        update: see also:
        DBI recipes
        Speeding up the DBI

Re: DBI use
by Grygonos (Chaplain) on Nov 16, 2004 at 13:41 UTC

    Please go to the super search section of this site and search DBI placeholder. It's just good practice to get into. DBI @ cpan.org might have info in the POD on placeholders as well.

Re: DBI use
by erix (Prior) on Nov 16, 2004 at 16:44 UTC

    Sorry Win, My last remarks on this stuff:

    In my first reply, I have missed the following errors:

    $command (an sql select I suppose?) is passed to the sub, and prepared. Then an execute should follow but you do first another prepare, on another sql statement. Make up your mind which sql statement to prepare, execute, and fetch.

    If you are doing stored procedures, say what DBMS you are using.

    Do yourself a favor, read DBI recipes ...

Re: DBI use
by csuhockey3 (Curate) on Nov 16, 2004 at 18:21 UTC
    here is a recent thread about DBI you might find helpful.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://408094]
Approved by TedPride
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-25 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found