Solved

Perl DBI Insert Array of Arrays

Posted on 2010-11-09
3
1,253 Views
Last Modified: 2012-05-10
I am attempting to insert an array of arrays using a perl DBI statement.

I have tried both $sth->execute(@array) and $sth->execute_array(@array)

I know how to do this if I were to break the each sub-array into individual arrays, but the data is not being presented in that fashion.

What I have so far is:


use strict;
use warnings;
use DBI;

my @array = (
               ['NC', 1, 2, 3 ],
               ['SC', 4, 5, 6 ],
               ['NW', 7, 8, 9 ],
            );

my $dbh = DBI->connect("DBI:mysql:database", 
   username, password, {RaiseError => 1, PrintError => 0});

my $ins_q = "INSERT INTO Table VALUES 
    (NULL, CURRENT_DATE(), ?, ?, ?, ?)";

my $insert = $dbh->prepare($ins_q);
$dbh->execute(\@array);

Open in new window

0
Comment
Question by:_KrYPt0_
  • 2
3 Comments
 

Author Comment

by:_KrYPt0_
ID: 34108052
So I came up with the following work around

my @array = (

               ['NC', 1, 2, 3 ],

               ['SC', 4, 5, 6 ],

               ['NW', 7, 8, 9 ],

            );



my $dbh = DBI->connect("DBI:mysql:database", 

   username, password, {RaiseError => 1, PrintError => 0});



my $ins_q = "INSERT INTO Table VALUES 

    (NULL, CURRENT_DATE(), ?, ?, ?, ?)";



my $insert = $dbh->prepare($ins_q);

foreach my $row (@array) {

  $dbh->execute(@{$row});

}

Open in new window

0
 
LVL 16

Expert Comment

by:jmatix
ID: 34115730
Another option is to use execute_array(). But then you will have to transpose the array as:
use strict;

use warnings;

use DBI;



my @array = (

               ['NC', 1, 2, 3 ],

               ['SC', 4, 5, 6 ],

               ['NW', 7, 8, 9 ],

            );



my $dbh = DBI->connect("DBI:mysql:database", 

   username, password, {RaiseError => 1, PrintError => 0});



my $ins_q = "INSERT INTO Table VALUES 

    (NULL, CURRENT_DATE(), ?, ?, ?, ?)";



my $insert = $dbh->prepare($ins_q);

for $e (0..$#array){$tmp=$array[$e][$_], $array[$e][$_]=$array[$_][$e], $array[$_][$e]=$tmp for ($e+1..$#{$array[$e]})};



$insert->execute_array(undef, @array);



$dbh->disconnect;

Open in new window

0
 
LVL 16

Accepted Solution

by:
jmatix earned 250 total points
ID: 34115762
Since you are using strict, add my ($e, $tmp):
use strict;

use warnings;

use DBI;



my @array = (

               ['NC', 1, 2, 3 ],

               ['SC', 4, 5, 6 ],

               ['NW', 7, 8, 9 ],

            );



my $dbh = DBI->connect("DBI:mysql:database", 

   username, password, {RaiseError => 1, PrintError => 0});



my $ins_q = "INSERT INTO Table VALUES 

    (NULL, CURRENT_DATE(), ?, ?, ?, ?)";



my $insert = $dbh->prepare($ins_q);



my ($e, $tmp);

for $e (0..$#array){$tmp=$array[$e][$_], $array[$e][$_]=$array[$_][$e], $array[$_][$e]=$tmp for ($e+1..$#{$array[$e]})};



$insert->execute_array(undef, @array);



$dbh->disconnect;

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now