Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Perl - DBI - How to process array rather than single value?

by sundialsvc4 (Abbot)
on Apr 10, 2014 at 12:29 UTC ( [id://1081808]=note: print w/replies, xml ) Need Help??


in reply to Perl - DBI - How to process array rather than single value?

Three short answers:

  1. If you need the results of an SQL query to appear in a particular sequence, let the SQL engine do it via ORDER BY.
  2. If you do get an arrayref as the result, use foreach to iterate through it.
  3. However, the arrayref approach would be problematic for a large number of records since all of them would be stored in your program’s memory first.   You can avoid this issue by retrieving the rows one row at a time, say into a hashref where the hash-keys are the column names, using an ordinary while loop.

There are, of course, copious examples of this on the Internet, both here and elsewhere and to a certain extent within the documentation and manuals for DBI.   (Click on the product-name at the top of that page, right next to the package-author’s name, to see the full list of help-topics associated with the package.   Do a search on the package-name to find even more goodies.)   Usually, you can quickly solve questions like these by surfing for an existing code-example ... a very powerful strategy otherwise not-so diplomatically known as:   “RTFM.”   ;-)

Replies are listed 'Best First'.
Re^2: Perl - DBI - How to process array rather than single value?
by chacham (Prior) on Apr 10, 2014 at 14:46 UTC

    Considering the query uses '?', i assume it is a placeholder. It seems that he is executing the query with a single value a number of times, and after each run he does something with the returned values.

    Afterward, he has an array of IDs @queryIDs. He wants to execute the the same query (and gather the results in order) for the values in the array.

    This is actually a SQL question, and the answer is, it depends. The quick and dirty (and insecure) method is to join the array with commas and build a dynamic SQL statement with IN().

    To do the same securely, would require splitting the list in SQL, which depends on the RDBMS and version. For example, Oracle 11r2 has a good tokenization method via XMLDB which can be found with a simple search.

Log In?
Username:
Password:

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

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

    No recent polls found