[PyGreSQL] How to insert a record if it doesn't exist else update it ?
D'Arcy J.M. Cain
darcy at PyGreSQL.org
Wed Dec 19 11:04:07 EST 2007
On Wed, 19 Dec 2007 23:39:34 +0800
"fdu.xiaojf at gmail.com" <fdu.xiaojf at gmail.com> wrote:
> Hi all,
>
> What I want to do is:
> if a record already exists in the table, update it
> if a record doesn't exist, insert it into the table
First of all, this is a PostgreSQL question, not a PyGreSQL one. In
general you will get much better responses if you use the correct
mailing list. Check out http://www.PostgreSQL.org/ for their mailing
lists. You probably want the SQL or help list.
> I know that there is a ON DUPLICATE clause with MySQL, so I'm wondering is
> there a corresponding way to do this in PostgreSQL ?
I don't think so but you can probably do something with rules. Check
out the PostgreSQL docs.
> Currently the only way I can find is do query first and then update or insert.
I think that you will find this the safest way. If you know that you
aren't in the middle of a transaction you could use a try/except
statement but this will break your transaction if you are in one.
--
D'Arcy J.M. Cain
PyGreSQL Development Group
http://www.PyGreSQL.org
More information about the PyGreSQL
mailing list