I want to know what is the difference between drupal_write_record and db_insert example : can i translate that drupal_write_record('MyCustomTable', $Myrecord, $primary_key); to an insert query ?

thank you


drupal_write_record doesn't know by itself whether to do the insert or the update, but this is determined by the existence (or not) of the 3rd parameter: $primary_keys which defaults to array() while db_insert is to insert new record to database.

according to drupal_write_record API documentation

$primary_keys: To indicate that this is a new record to be inserted, omit this argument. If this is an update, this argument specifies the primary keys' field names. If there is only 1 field in the key, you may pass in a string; if there are multiple fields in the key, pass in an array.


Do not use drupal_write_record() within hook_update_N() functions, since the database schema cannot be relied upon when a user is running a series of updates. Instead, use db_insert() or db_update() to save the record.

  • Thank you, but can i replace a drupal_write_record by an insert ?
    – Mitch
    Jul 27 '15 at 15:14
  • 2
    Not entirely, but you could with a db_merge @Mitch
    – Clive
    Jul 27 '15 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.