Answers.Body: <p>Agree, the API is decidedly the best way as it does multiple stuff and should continue to function over upgrades when there are database changes. I only use SQL when either the API does not do what I need, with custom data because it is easier most of the times or when doing bulk stuff.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-12T17:56:45.540Z
Answers.LastActivityDate: 2015-08-12T17:56:45.540Z
Answers.UserDisplayName: ErikH - CiviCooP
Answers.Body: <p>Generally, it is wisest to always use the API.  Many necessary updates actually touch multiple tables and the API takes care of it nicely.  This is especially true with financial updates. The API also encapsulates the database structure so as to effectively hide any database structure changes that may happen over time.</p>
  
  <p>IMHO, pretty much the only time that SQL might be called for is if you are doing a huge bulk update wherein using the API would take excessively long.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-12T17:40:34.807Z
Answers.LastActivityDate: 2015-08-12T17:40:34.807Z
Answers.UserDisplayName: DaveD
Body: <p>Simple question here,
  From a computing and 'civicrm correctness' point of view, is using SQL (via CRM_Core_DAO::executeQuery or similar) or using the API ( civicrm_api3('Contact', 'create', $params)) better?
  Thanks,
  V</p>
  
Tags: <api><sql>
Title: For an extension, to modify database data, is it better to use SQL or API
ThreadText: # For an extension, to modify database data, is it better to use SQL or API
  
  (Asked by: username_0 on 2015-08-12)
  
  Simple question here,
  From a computing and 'civicrm correctness' point of view, is using SQL (via CRM\_Core\_DAO::executeQuery or similar) or using the API ( civicrm\_api3('Contact', 'create', $params)) better?
  Thanks,
  V
  
  **Tags:** api, sql
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-08-12. Score: 3 | Accepted answer
  
  Generally, it is wisest to always use the API. Many necessary updates actually touch multiple tables and the API takes care of it nicely. This is especially true with financial updates. The API also encapsulates the database structure so as to effectively hide any database structure changes that may happen over time.
  
  IMHO, pretty much the only time that SQL might be called for is if you are doing a huge bulk update wherein using the API would take excessively long.
  
  
  ---
  
  ### Answer by username_1 on 2015-08-12. Score: 2
  
  Agree, the API is decidedly the best way as it does multiple stuff and should continue to function over upgrades when there are database changes. I only use SQL when either the API does not do what I need, with custom data because it is easier most of the times or when doing bulk stuff.
  
  
  ---
  
  