Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old May 14th, 2008, 05:10 PM
Familiar Sight
 
Join Date: Jan 2008
Posts: 170
Default How to insert 2 arrays into 2 fields in MySQL using Perl

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.  
Reply
  #2  
Old May 14th, 2008, 05:34 PM
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
Default

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
Reply
  #3  
Old May 15th, 2008, 04:15 AM
Familiar Sight
 
Join Date: Jan 2008
Posts: 170
Default

alright thanks
once again perl guys
i need your help once again
:)
Reply
  #4  
Old May 15th, 2008, 05:02 AM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Age: 22
Posts: 2,790
Default

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
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles