Connecting Tech Pros Worldwide Forums | Help | Site Map

How to insert 2 arrays into 2 fields in MySQL using Perl

Familiar Sight
 
Join Date: Jan 2008
Posts: 170
#1: May 14 '08
hi guys,

i dunno if this post should be in Perl or MySQL

but i'm using Perl DBI to do manupilations now in MySQL

i've got problem trying to input 2 arrays of data into 2 fields at the same time

say for example now i got
Expand|Select|Wrap|Line Numbers
  1.  
  2. @array1 = [ 1 ,2 , 3];
  3. @array2 = [4, 5, 6];
  4.  
and i got say 2 fields User and Time

i tried using this code
Expand|Select|Wrap|Line Numbers
  1. foreach $user(@array1){
  2. $sth -> $dbh ->perpare("INSERT INTO `trial` (User) VALUES ('$user')");
  3. $sth->execute();
  4.  
  5. foreach $time(@array2){
  6. $sth -> $dbh ->perpare("INSERT INTO `trial` (Time) VALUES ('$time')");
  7. $sth->execute();
  8.  
i got the results like:
Expand|Select|Wrap|Line Numbers
  1. User    Time
  2. 1          NULL
  3. 2          NULL
  4. 3          NULL
  5. NULL    4
  6. NULL    5
  7. NULL    6
  8.  
how should i change or input my code so i can get this in the database
Expand|Select|Wrap|Line Numbers
  1. User      Time
  2. 1           4
  3. 2           5
  4. 3           6
  5.  

ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: May 14 '08

re: How to insert 2 arrays into 2 fields in MySQL using Perl


This is a coding problem, i.e. a Perl question and I do not think we in the MySQL forum can write the Perl code for that. So this thread is moved to the Perl forum.

moderator
Familiar Sight
 
Join Date: Jan 2008
Posts: 170
#3: May 15 '08

re: How to insert 2 arrays into 2 fields in MySQL using Perl


alright thanks
once again perl guys
i need your help once again
:)
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#4: May 15 '08

re: How to insert 2 arrays into 2 fields in MySQL using Perl


I'm not a Perl person, but MySQL can insert into multiple columns and rows in one query:
For example:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO userTable(UserID, Time) VALUES
  2. (1, 4), (2, 5), (3, 6);
  3.  
Try re-writing your Perl code to take advantage of that.
Reply