Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^5: Multiple queries on DBI corresponding to multiple csv files?

by jtech (Sexton)
on Feb 21, 2019 at 10:54 UTC ( [id://1230297]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Multiple queries on DBI corresponding to multiple csv files?
in thread Multiple queries on DBI corresponding to multiple csv files?

That piece of code is returning only the last id. How to get all ids using it?

I have changed it for the below code but now it is returning only the first element. Can you help me, please?

if ($id[0] eq "") { my $sql = "SELECT DISTINCT id FROM mytable WHERE active = ?"; $id[0] = @{$dbh->selectall_arrayref($sql,undef,'no')}; #@id = map{ $_->[0] } @$ref; print join (", ", @id), "\n"; }
  • Comment on Re^5: Multiple queries on DBI corresponding to multiple csv files?
  • Download Code

Replies are listed 'Best First'.
Re^6: Multiple queries on DBI corresponding to multiple csv files?
by hippo (Bishop) on Feb 21, 2019 at 11:18 UTC
    I have changed it for the below code but now it is returning only the first element.
    $id[0] = @{$dbh->selectall_arrayref($sql,undef,'no')};

    It's returning all the elements but you are then discarding all but the first. If you want them all, keep them all:

    @id = @{$dbh->selectall_arrayref($sql,undef,'no')};

      It seems I am not able to integrate array, scalar, reference and variables using an undefined "" parameter.

      The code explains better than me:

      # When it is not typed, the arguments will start as "" blank # (which means all ids) @id = ""; # Check typed argument ("-id1", duplicated or none) foreach my $arg (@ARGV) { if ($arg =~ /^--?id(\d+)$/) { die "Error: Multiple ids specified: -id@id and $arg.\n" if ( +$id[0]); @id = $1 ; next; } } # Exec problem if ($id[0] eq "") { my $sql = "SELECT DISTINCT id FROM mytable WHERE active = ?"; @id = @{$dbh->selectall_arrayref($sql,undef,'no')}; print join (", ", @id), "\n"; }

      Output error:

      Running SELECT id FROM mytable WHERE id=? AND salary >= 1 for ARRA +Y(0x1011e70) ID: ARRAY(0x1011e70), 1 lines dumped to csvfile1_ARRAY(0x1011e70).csv

      Output error when replaced $id[0] to @id at if conditional: if (@id eq "")

      Argument "" isn't numeric in subroutine entry at ./test.pl line 108.
        ->selectall_arrayref() returns a reference to an Array of Arrays

        [ [field1,field2,etc],[field1,field2,etc],[field1,field2,etc]] so you need the map to extract the first field from each record. Your query only returns one field but the structure is the same.

        my $ref = $dbh->selectall_arrayref($sql,undef,'no'); @id = map{ $_->[0] } @$ref;
        poj

      Please ignore it, I don't have a clue what I am doing

      The code was always right, just realized the where clause has bitten me here. lol

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-26 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found