pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
[newbie] How to do "batch insert"?
2+ messages / 2 participants
[nested] [flat]

* [newbie] How to do "batch insert"?
@ 2007-06-08 08:44  李彦 Ian Li <liyan82@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: 李彦 Ian Li @ 2007-06-08 08:44 UTC (permalink / raw)
  To: pgsql-php

Hi all.
In the scene I want to insert all elements of an array in to a 
table, how can I do to commit them as a whole?
currently I do like this:

<?php
$result = pg_prepare($dbconn, "ins_acl", 'INSERT INTO 
acls(oid,uid,gid,mod) VALUES($1,$2,$3,$4)');
for($i=0;$i<count($p["acls"]);$i++){
	pg_execute($dbconn, "ins_lgs",$p["acls"][i]);
}
?>

I wonder while doing so, will it commit in each LOOP or just ONCE?

Thanks and Regards,

Ian



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: [newbie] How to do "batch insert"?
@ 2007-06-08 10:34  Mihail Mihailov <Mihail.Mihailov@uta.fi>
  parent: 李彦 Ian Li <liyan82@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Mihail Mihailov @ 2007-06-08 10:34 UTC (permalink / raw)
  To: pgsql-php

Hi Ian,

It should be much easier to use pg_query function.

pg_query("START TRANSACTION");
  for (......)
   {
     $your_insert_query = "INSERT INTO acls(oid,uid,gid,mod)  
VALUES($1,$2,$3,$4)"
     $r = pg_query($your_insert_query);
        if (!$r) //Query failed
         {
            //issue error message
           // exit loop, all changes in the table will not be saved
         }
    }
pg_query("COMMIT"); //Commit all changes in the table

If you mark the transaction block ("start transaction" ... "commit")  
the changes will be commited only after the script reaches "commit"  
clause. If it is aborted before, no changes will be saved. This is the  
purpose of transaction (which is also the way to increase the speed of  
the program execution).

- Mike

Quoting 李彦 Ian Li <liyan82@gmail.com>:

> Hi all.
> In the scene I want to insert all elements of an array in to a table,
> how can I do to commit them as a whole?
> currently I do like this:
>
> <?php
> $result = pg_prepare($dbconn, "ins_acl", 'INSERT INTO
> acls(oid,uid,gid,mod) VALUES($1,$2,$3,$4)');
> for($i=0;$i<count($p["acls"]);$i++){
> 	pg_execute($dbconn, "ins_lgs",$p["acls"][i]);
> }
> ?>
>
> I wonder while doing so, will it commit in each LOOP or just ONCE?
>
> Thanks and Regards,
>
> Ian
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
>
>               http://www.postgresql.org/docs/faq





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2007-06-08 10:34 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-06-08 08:44 [newbie] How to do "batch insert"? 李彦 Ian Li <liyan82@gmail.com>
2007-06-08 10:34 ` Mihail Mihailov <Mihail.Mihailov@uta.fi>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox