Maxipoint Rep Office wrote:
> How insert data with Perl into multiple MySQL tables?

You need multiple INSERT statements. Use a transaction if they all need to
complete or fail together. I don't use MySQL, so I don't know how
transactions work for it specifically, but typically you just use a
combination of $dbh->{AutoCommit} and $dbh->commit.

> 
> I can not find any clear advice..
> 
> 
> this is for TABLE1:
> 
> # Connect to the database.
> my $dbh = DBI->connect("DBI:mysql:database=xxxx;host=localhost",
>                        "username", "pass",
>                        {'RaiseError' => 1});
> 
> 
> # now first make that sql statement the right way - use placeholders
> 
> my $sql = "INSERT INTO TABLE1 (xxx1, xxx2, xxx3) VALUES (?,?,?)";
> 
> 
> #now prepare it
> my $sth = $dbh->prepare($sql);
> 
> 
> # now simply execute it
> 
> $sth->execute($f->{xxx1},$f->{xxx2},$f->{xxx3});
> 
> 
> $sth->finish();

finish() is only needed for statements that return rows.

Also the prepare/execute can all be simplified to

    $dbh->do(q[
        insert into table1 (xxx1, xxx2, xxx3) values (?, ?, ?)
    ], undef, @{$f}{qw(xxx1 xxx2 xxx3)});

Using prepare separately is handy if you need to repeat the insert multiple
times for different data; otherwise, using $dbh->do is usually simpler.

> 
> $dbh->disconnect();
> 
> 
> 
> How insert into TABLE1 and TABLE2 at same time?

You need to prepare/execute (or $dbh->do) another statement for that.

   $dbh->do(q[insert into table2 ...blah blah], undef, @params);

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to