How add/update a record conditionally based on existence of primary key? I have a table with structure:

Table
(
record_1 int,
record_2 int,
PRIMARY KEY (record_1)
);

I want to write/update a record based on its existence.

I was told to use drupal_write_record(), but for that I must know if the record already exists or not.

In my case I am not aware of its existence.

How can I do this?
No, you do not "must know if the record already exists or not". If you will look at drupal_write_record source code, you will see these lines:

 if (empty($primary_keys)) {
    // We are doing an insert.


On else, this function will update instead of insert. The only problem with this is that Drupal's record system expects you will leave key management to Drupal. No saving at arbitrary key, because it breaks autoincrement.

If you need to manage keys on your own, use lower level db_merge function instead.