Use db_merge instead of drupal_write_record to avoid PDO errors