DBI:mysql how to do multiple inserts/modification?

Discussion in 'Perl Misc' started by Tomasz Chmielewski, Feb 13, 2008.

  1. How can I do multiple database modifications without writing too much code?

    For a single modification, I would write:

    $SQL = "INSERT INTO aaaaa..";
    $db->do($SQL);

    How can I do it without repeating "$db->do($SQL);" each time?


    Something like this throws an error, so obviously I must be missing
    something.

    $SQL = "INSERT INTO aaaaa... ;
    INSERT INTO bbbbb... ;
    INSERT INTO bbbbb... ;";
    $db->do($SQL);



    --
    Tomasz Chmielewski
    http://wpkg.org
     
    Tomasz Chmielewski, Feb 13, 2008
    #1
    1. Advertising

  2. Tomasz Chmielewski

    Paul Lalli Guest

    On Feb 13, 9:03 am, Tomasz Chmielewski <>
    wrote:
    > How can I do multiple database modifications without writing too much code?
    >
    > For a single modification, I would write:
    >
    > $SQL = "INSERT INTO aaaaa..";
    > $db->do($SQL);
    >
    > How can I do it without repeating "$db->do($SQL);" each time?
    >
    > Something like this throws an error, so obviously I must be missing
    > something.
    >
    > $SQL = "INSERT INTO aaaaa... ;
    >         INSERT INTO bbbbb... ;
    >         INSERT INTO bbbbb... ;";
    > $db->do($SQL);


    uh. How about...

    for my $stmt (split /;\n/, $SQL) {
    $db->do($stmt);
    }

    Paul Lalli
     
    Paul Lalli, Feb 13, 2008
    #2
    1. Advertising

  3. Tomasz Chmielewski

    John Bokma Guest

    Tomasz Chmielewski <> wrote:

    > How can I do multiple database modifications without writing too much
    > code?
    >
    > For a single modification, I would write:
    >
    > $SQL = "INSERT INTO aaaaa..";
    > $db->do($SQL);
    >
    > How can I do it without repeating "$db->do($SQL);" each time?


    prepare the statement, and in a loop do $sth->execute( values )

    --
    John

    Arachnids near Coyolillo - part 1
    http://johnbokma.com/mexit/2006/05/04/arachnids-coyolillo-1.html
     
    John Bokma, Feb 13, 2008
    #3
  4. Paul Lalli schrieb:
    > On Feb 13, 9:03 am, Tomasz Chmielewski <>
    > wrote:
    >> How can I do multiple database modifications without writing too much code?
    >>
    >> For a single modification, I would write:
    >>
    >> $SQL = "INSERT INTO aaaaa..";
    >> $db->do($SQL);
    >>
    >> How can I do it without repeating "$db->do($SQL);" each time?
    >>
    >> Something like this throws an error, so obviously I must be missing
    >> something.
    >>
    >> $SQL = "INSERT INTO aaaaa... ;
    >> INSERT INTO bbbbb... ;
    >> INSERT INTO bbbbb... ;";
    >> $db->do($SQL);

    >
    > uh. How about...
    >
    > for my $stmt (split /;\n/, $SQL) {
    > $db->do($stmt);
    > }


    Ah, that simple.
    Thanks a lot.


    --
    Tomasz Chmielewski
    http://wpkg.org
     
    Tomasz Chmielewski, Feb 13, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derick van Niekerk
    Replies:
    8
    Views:
    497
    Carsten Haese
    Jan 27, 2006
  2. Paul Vudmaska

    dbi:mysql mysql has gone away

    Paul Vudmaska, Apr 21, 2004, in forum: Ruby
    Replies:
    0
    Views:
    125
    Paul Vudmaska
    Apr 21, 2004
  3. Jerome Hauss
    Replies:
    0
    Views:
    188
    Jerome Hauss
    Oct 13, 2004
  4. Replies:
    1
    Views:
    197
  5. Replies:
    7
    Views:
    365
Loading...

Share This Page