-1

Im converting my php app to work with postgresql instead of mysql.

Before I had this function using PDO:

$stmt = $db->prepare("INSERT INTO $table(id,name,info) VALUES(:id,:name,:info) ON DUPLICATE KEY UPDATE name=:name,info=:info");
$stmt->execute(array(':id' => $id, ':name' => $name, :info' => $info));

If the key does not exist it creates a new entry, or else it updates the entry.

How is it possible to do somewhat the same with postgresql?

I have read some articles, but no one is clear on what I have to do. Do I need to write an trigger or rule for this?

Or do I write an check in php if the entry/id exist, and determine if it should update or create new?

Im using postgres 9.1.8

Tomas Jacobsen
  • 2,368
  • 6
  • 37
  • 81
  • 2
    http://www.depesz.com/2012/06/10/why-is-upsert-so-complicated/ – Craig Ringer Mar 25 '13 at 11:44
  • Could someone explain where I use the "CREATE FUNCTION" ? Do I do it in the psql shell, make a file on the server, use it in php? Do I have to write function for every table I need to update? – Tomas Jacobsen Mar 25 '13 at 12:40

1 Answers1

1

http://en.wikipedia.org/wiki/Upsert

You can run the merge command trought the prepare then execute it.

I guess this should work

Eernie
  • 470
  • 3
  • 15