Array

F

Faith Greenwood

Hi,

Seems like I've been googling for hours and can't find the solution.
Maybe someone here can help:

I have 3 arrays, each with 3 items:
@array1=(1,2,3); #names
@array2=(4,5,6); #email addresses
@array3=(7,8,9); #cities

I need to put the values of each into a database, in order. This is
the idea I have so far, which is obviously wrong:

foreach (@array1){
my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
my $sth = $dbh->prepare($sql);
$sth->execute($array1,$array2,$array3);
}

In other words I want to put the array items in the 'example' table so
that I have the following 3 records in my database:

####Record 1
name: 1, email: 4, city: 7;
####Record 2
name: 2, email: 5, city: 8;
####Record 3
name: 3, email: 6, city: 9;

How would I do that?
 
A

A. Sinan Unur

Seems like I've been googling for hours and can't find the solution.
Maybe someone here can help:

I have 3 arrays, each with 3 items:
@array1=(1,2,3); #names
@array2=(4,5,6); #email addresses
@array3=(7,8,9); #cities

Don't do that:

my @customers = (
{ name => 1, email => 4, city => 7 },
{ name => 2, email => 5, city => 8 },
{ name => 3, email => 6, city => 9 },
);
I need to put the values of each into a database, in order. This is
the idea I have so far, which is obviously wrong:

foreach (@array1){
my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
my $sth = $dbh->prepare($sql);
$sth->execute($array1,$array2,$array3);
}

There are no variables $array1, $array2, $array3 in your script.

Untested code follows:

use List::MoreUtils qw( each_array );

my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
my $sth = $dbh->prepare($sql);

my $it = each_array( @array1, @array2, @array3 );
while ( my ( $name, $email, $city ) = $it->() ) {
$st->execute( $name, $email, $city );
}

In the future, choose better data structures.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
S

smallpond

Hi,

Seems like I've been googling for hours and can't find the solution.
Maybe someone here can help:

I have 3 arrays, each with 3 items:
@array1=(1,2,3);  #names
@array2=(4,5,6);  #email addresses
@array3=(7,8,9); #cities

I need to put the values of each into a database, in order. This is
the idea I have so far, which is obviously wrong:

foreach (@array1){
my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
my $sth = $dbh->prepare($sql);
$sth->execute($array1,$array2,$array3);

}

In other words I want to put the array items in the 'example' table so
that I have the following 3 records in my database:

####Record 1
name: 1, email: 4, city: 7;
####Record 2
name: 2, email: 5, city: 8;
####Record 3
name: 3, email: 6, city: 9;

How would I do that?

You need to learn how arrays work. The array
as a whole is called @array1. The individual
elements in the array are $array1[0], $array1[1] ...
$array1 is, confusingly, an unrelated scalar
variable. That's how perl works.

You only need to do the prepare once.

my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
my $sth = $dbh->prepare($sql); # error check?

Then execute 3 times in a for loop:

for ($i=0; $i<3; $i++) {
$sth->execute($array1[$i], $array2[$i], $array3[$i]);
# error check?
}

You aren't checking for errors. If you don't care whether
your call succeeds or not, why do it at all?
 
F

Faith Greenwood

Seems like I've been googling for hours and can't find the solution.
Maybe someone here can help:
I have 3 arrays, each with 3 items:
@array1=(1,2,3); #names
@array2=(4,5,6); #email addresses
@array3=(7,8,9); #cities
I need to put the values of each into a database, in order. This is
the idea I have so far, which is obviously wrong:
foreach (@array1){
my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
my $sth = $dbh->prepare($sql);
$sth->execute($array1,$array2,$array3);

In other words I want to put the array items in the 'example' table so
that I have the following 3 records in my database:
####Record 1
name: 1, email: 4, city: 7;
####Record 2
name: 2, email: 5, city: 8;
####Record 3
name: 3, email: 6, city: 9;
How would I do that?

You need to learn how arrays work. The array
as a whole is called @array1. The individual
elements in the array are $array1[0], $array1[1] ...
$array1 is, confusingly, an unrelated scalar
variable. That's how perl works.

You only need to do the prepare once.

my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
my $sth = $dbh->prepare($sql); # error check?

Then execute 3 times in a for loop:

for ($i=0; $i<3; $i++) {
$sth->execute($array1[$i], $array2[$i], $array3[$i]);
# error check?

}

You aren't checking for errors. If you don't care whether
your call succeeds or not, why do it at all?


thx Sinan and smallpond. I ended up using smallpond's suggestion as it
seemed simpler to me. I do have error checking in my code, which is
200 lines. For simplicity I only posted the relevant parts of the
code that pertain to my question.

thx again guys!
 
P

Peter J. Holzer

Seems like I've been googling for hours and can't find the solution.

"perldoc DBI" almost always provides the right answer to the question
"how do I ... with DBI".
Maybe someone here can help:

I have 3 arrays, each with 3 items:
@array1=(1,2,3); #names
@array2=(4,5,6); #email addresses
@array3=(7,8,9); #cities

I need to put the values of each into a database, in order. This is
the idea I have so far, which is obviously wrong:

foreach (@array1){

You don't need the loop.
my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
my $sth = $dbh->prepare($sql);
$sth->execute($array1,$array2,$array3);

$sth->execute_array({}, \@array1, \@array2, \@array3);

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top