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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poolboi
    New Member
    • Jan 2008
    • 170

    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
    [CODE=perl]

    @array1 = [ 1 ,2 , 3];
    @array2 = [4, 5, 6];
    [/CODE]

    and i got say 2 fields User and Time

    i tried using this code
    [CODE=perl]
    foreach $user(@array1){
    $sth -> $dbh ->perpare("INSER T INTO `trial` (User) VALUES ('$user')");
    $sth->execute();

    foreach $time(@array2){
    $sth -> $dbh ->perpare("INSER T INTO `trial` (Time) VALUES ('$time')");
    $sth->execute();
    [/CODE]

    i got the results like:
    [CODE=perl]
    User Time
    1 NULL
    2 NULL
    3 NULL
    NULL 4
    NULL 5
    NULL 6
    [/CODE]

    how should i change or input my code so i can get this in the database
    [CODE=text]
    User Time
    1 4
    2 5
    3 6
    [/CODE]
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    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

    Comment

    • poolboi
      New Member
      • Jan 2008
      • 170

      #3
      alright thanks
      once again perl guys
      i need your help once again
      :)

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        I'm not a Perl person, but MySQL can insert into multiple columns and rows in one query:
        For example:
        [code=mysql]
        INSERT INTO userTable(UserI D, Time) VALUES
        (1, 4), (2, 5), (3, 6);
        [/code]
        Try re-writing your Perl code to take advantage of that.

        Comment

        Working...