This example demonstrates how to use the database API.
General documentation is available at Database abstraction layer.
The several examples here demonstrate basic database usage.
The usage of db_query() for INSERT, UPDATE, or DELETE is deprecated, because it is database-dependent. Instead, specific functions are provided to perform these operations: db_insert(), db_update(), and db_delete() do the job now. (Note that backdrop_write_record() is also deprecated.)
db_insert() example:
// INSERT INTO {database_example} (name, surname) VALUES('John, 'Doe')
db_insert('database_example')
->fields(array('name' => 'John', 'surname' => 'Doe'))
->execute();
db_update() example:
// UPDATE {database_example} SET name = 'Jane' WHERE name = 'John'
db_update('database_example')
->fields(array('name' => 'Jane'))
->condition('name', 'John')
->execute();
db_delete() example:
<?php
// DELETE FROM {database_example} WHERE name = 'Jane'
db_delete(database_example')
->condition('name', 'Jane')
->execute();
?>
See Database Abstraction Layer
See also
Parent topics
File
- modules/
examples/ database_example/ database_example.module, line 9 - Hook implementations for the Database Example module.
Functions
Name | Location | Description |
---|---|---|
database_example_advanced_list |
modules/ |
Renders a filtered list of entries in the database. |
database_example_entry_delete |
modules/ |
Deletes an entry from the database. |
database_example_entry_insert |
modules/ |
Saves an entry in the database using db_insert(). |
database_example_entry_load |
modules/ |
Reads from the database using a filter array. |
database_example_entry_update |
modules/ |
Updates an entry in the database. |
database_example_form_add |
modules/ |
Prepares a simple form to add an entry, with all the interesting fields. |
database_example_form_add_submit |
modules/ |
Submit handler for 'add entry' form. |
database_example_form_update |
modules/ |
Sample UI to update a record. |
database_example_form_update_callback |
modules/ |
AJAX callback handler for the pid select. |
database_example_form_update_submit |
modules/ |
Submit handler for the 'update entry' form. |
database_example_install |
modules/ |
Implements hook_install(). |
database_example_list |
modules/ |
Renders a list of entries in the database. |
database_example_menu |
modules/ |
Implements hook_menu(). |
database_example_schema |
modules/ |
Implements hook_schema(). |
Classes
Name | Location | Description |
---|---|---|
DatabaseExampleUnitTestCase |
modules/ |
Default test case for the Database Example module. |