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 ');