How to backport db_merge() to drupal 6?
From the MergeQuery docs

An ANSI SQL:2003 compatible database would run the following query:

MERGE INTO table_name_1 USING table_name_2 ON (condition)
  WHEN MATCHED THEN
  UPDATE SET column1 = value1 [, column2 = value2 ...]
  WHEN NOT MATCHED THEN
  INSERT (column1 [, column2 ...]) VALUES (value1 [, value2 ...

Other databases (most notably MySQL, PostgreSQL and SQLite) will emulate this statement by running a SELECT and then INSERT or UPDATE.



You need to do a SELECT using db_query first, to find out if your data already exists.
Then you can use drupal_write_record, which will do either an UPDATE or an INSERT depending on whether or not you populate the tables primary key(s).