Common examples of database operations in DRUPAL7

Source: Internet
Author: User

One of the new features that

 drupal 7 provides is the ability to construct query objects using Query Builder and query Objects, eliminating the need to write the original SQL statements in code, and improving the readability of the Code, The second is compatible with all databases

1. Insert a single record   code as follows: Db_insert ("table")->fields (Array (' field1 ' => ' value1 ', ' field2 ' => ' value2 '), ' fieldn ' =& Gt $valuen))->execute ());   2. Insert more than one record     code as follows: $values [] = Array (' field1 ' => ' val1 ', ' field2 ' => ' val2 ', ' fieldn ' => $valn); $values [] = Array (' field1 ' => ' value1 ', ' field2 ' => ' value2 ', ' fieldn ' => $valuen); $query = Db_insert (' table ')->fields (Array (' field1 ', ' field2 ', ' fieldn ')); foreach ($values as $record) {    $query->values ($record);} $query->execute ();   3. Update a record code as follows: Db_update (' Imports ')    ->condition (' name ', ' Chico ')    ->fields (array (' Address ' => ' to West St. ')    ->execute (); Equivalent to:   UPDATE {Imports} SET address = ' Go West St. ' WHERE name = ' Chico ';   4. Delete a record     code as follows: Db_delete (' Imports ')    ->condition (' name ' => ' Zeppo ')    - >execute (); 5. Merge records     code as follows: Db_merge (' People ')  ->key (' job ' => ' Speaker ')  ->insertfields (Array (' Age ' =>, ' name ' => ' Meredith '))  ->updatefields ( Array (' name ' => ' Tiffany '))  ->execute (); If there is a record with job speaker, update name is Tiffany, and if it does not, insert a record of age 31,name to meredith,job as speaker.     6. Automatically add one or more to the value of a field in a database.   Code as follows: Db_update (' example_table ')  ->expression (' count ', ' count + 1 ')  ->condition (' Field1 ', $ some_value)  ->expression (' field2 ', ' Field2 +: Inc ', Array (': Inc ' => 2))  ->execute ();   7. The query database is given another alias (alias)     code as follows: $query = Db_select (' node ', ' n '); $query->addfield (' n ', ' name ', ' label '); $query->addfield (' n ', ' name ', ' value ');  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.