1.插入单条记录
代码如下:
fields(array('field1' => 'value1','field2' => 'value2','fieldn' => $valuen))->execute();
2.插入多条记录
代码如下:
'val1','field2' => 'val2','fieldn' => $valn);
$values[] = array('field1' => 'value1','fieldn' => $valuen);
$query = db_insert(‘table')->fields(array('field1','field2','fieldn'));
foreach ($values as $record) {
$query->values($record);
}
$query->execute();
$values[] = array('field1' => 'value1','fieldn' => $valuen);
$query = db_insert(‘table')->fields(array('field1','field2','fieldn'));
foreach ($values as $record) {
$query->values($record);
}
$query->execute();
3.更新某条记录
代码如下:
->condition('name','Chico')
->fields(array('address' => 'Go West St.'))
->execute();
//等同于:
->fields(array('address' => 'Go West St.'))
->execute();
//等同于:
UPDATE {imports} SET address = 'Go West St.' WHERE name = 'Chico';
4.删除某条记录
代码如下:
->condition('name' => 'Zeppo')
->execute();
->execute();
5.合并记录
代码如下:
->key(array('job' => 'Speaker'))
->insertFields(array('age' => 31,'name' => 'Meredith'))
->updateFields(array('name' => 'Tiffany'))
->execute();
//如果存在job为Speaker的一条记录,则更新name为Tiffany,如果不存在,就插入一条age为31,name为Meredith,job为Speaker的记录。
->insertFields(array('age' => 31,'name' => 'Meredith'))
->updateFields(array('name' => 'Tiffany'))
->execute();
//如果存在job为Speaker的一条记录,则更新name为Tiffany,如果不存在,就插入一条age为31,name为Meredith,job为Speaker的记录。
代码如下:
->expression('count','count + 1')
->condition('field1',$some_value)
->expression('field2','field2 + :inc',array(':inc' => 2))
->execute();
->condition('field1',$some_value)
->expression('field2','field2 + :inc',array(':inc' => 2))
->execute();
代码如下:
$query->addField('n','name','label');
$query->addField('n','value');
$query->addField('n','value');