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

db_insert()

db_update()

db_delete()

backdrop_write_record()

Parent topics

File

modules/examples/database_example/database_example.module, line 9
Hook implementations for the Database Example module.

Functions

Namesort descending Location Description
database_example_advanced_list modules/examples/database_example/database_example.module Renders a filtered list of entries in the database.
database_example_entry_delete modules/examples/database_example/database_example.module Deletes an entry from the database.
database_example_entry_insert modules/examples/database_example/database_example.module Saves an entry in the database using db_insert().
database_example_entry_load modules/examples/database_example/database_example.module Reads from the database using a filter array.
database_example_entry_update modules/examples/database_example/database_example.module Updates an entry in the database.
database_example_form_add modules/examples/database_example/database_example.module Prepares a simple form to add an entry, with all the interesting fields.
database_example_form_add_submit modules/examples/database_example/database_example.module Submit handler for 'add entry' form.
database_example_form_update modules/examples/database_example/database_example.module Sample UI to update a record.
database_example_form_update_callback modules/examples/database_example/database_example.module AJAX callback handler for the pid select.
database_example_form_update_submit modules/examples/database_example/database_example.module Submit handler for the 'update entry' form.
database_example_install modules/examples/database_example/database_example.install Implements hook_install().
database_example_list modules/examples/database_example/database_example.module Renders a list of entries in the database.
database_example_menu modules/examples/database_example/database_example.module Implements hook_menu().
database_example_schema modules/examples/database_example/database_example.install Implements hook_schema().

Classes

Namesort descending Location Description
DatabaseExampleUnitTestCase modules/examples/database_example/tests/database_example.test Default test case for the Database Example module.