[newbie] How to do "batch insert"?
am 08.06.2007 10:44:10 von liyan82
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:
$result = pg_prepare($dbconn, "ins_acl", 'INSERT INTO
acls(oid,uid,gid,mod) VALUES($1,$2,$3,$4)');
for($i=0;$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
Re: [newbie] How to do "batch insert"?
am 08.06.2007 12:34:40 von Mihail Mihailov
Hi Ian,
It should be much easier to use pg_query function.
pg_query("START TRANSACTION");
for (......)
{
$your_insert_query =3D "INSERT INTO acls(oid,uid,gid,mod)
VALUES($1,$2,$3,$4)"
$r =3D 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=20=
=20
purpose of transaction (which is also the way to increase the speed of=20=
=20
the program execution).
- Mike
Quoting æ彦 Ian Li :
> 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:
>
>
> $result =3D pg_prepare($dbconn, "ins_acl", 'INSERT INTO
> acls(oid,uid,gid,mod) VALUES($1,$2,$3,$4)');
> for($i=3D0;$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
---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly