You are here

Example: Database (DBTNG) in Examples for Developers 7

Same name and namespace in other branches
  1. 8 dbtng_example/dbtng_example.module \dbtng_example
  2. 3.x modules/dbtng_example/dbtng_example.module \dbtng_example

Database examples, including DBTNG.

'DBTNG' means 'Database: The Next Generation.' Yes, Drupallers are nerds.

General documentation is available at database abstraction layer documentation and at Database API.

The several examples here demonstrate basic database usage.

In Drupal 6, the recommended method to save or update an entry in the database was drupal_write_record() or db_query().

In Drupal 7 and forward, 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 drupal_write_record() is also deprecated.)

db_insert() example:

// INSERT INTO {dbtng_example} (name, surname) VALUES('John, 'Doe')
db_insert('dbtng_example')
  ->fields(array(
  'name' => 'John',
  'surname' => 'Doe',
))
  ->execute();

db_update() example:

// UPDATE {dbtng_example} SET name = 'Jane' WHERE name = 'John'
db_update('dbtng_example')
  ->fields(array(
  'name' => 'Jane',
))
  ->condition('name', 'John')
  ->execute();

db_delete() example:

// DELETE FROM {dbtng_example} WHERE name = 'Jane'
db_delete('dbtng_example')
  ->condition('name', 'Jane')
  ->execute();

See Database Abstraction Layer

See also

db_insert()

db_update()

db_delete()

drupal_write_record()

Parent topics

File

dbtng_example/dbtng_example.module, line 15
This is an example outlining how a module can make use of the new DBTNG database API in Drupal 7.

Functions

Namesort descending Location Description
dbtng_example_advanced_list dbtng_example/dbtng_example.module Render a filtered list of entries in the database.
dbtng_example_convert_resultset_to_table_render_array dbtng_example/dbtng_example.module This function renders array for table 'dbtng_example'
dbtng_example_entry_delete dbtng_example/dbtng_example.module Delete an entry from the database.
dbtng_example_entry_insert dbtng_example/dbtng_example.module Save an entry in the database.
dbtng_example_entry_load dbtng_example/dbtng_example.module Read from the database using a filter array.
dbtng_example_entry_update dbtng_example/dbtng_example.module Update an entry in the database.
dbtng_example_execute_group_by_select_query dbtng_example/dbtng_example.module The code below will result in the following query SELECT ex.pid AS pid, ex.uid AS uid, ex.name AS name, ex.surname AS surname, ex.age AS age FROM {dbtng_example} ex GROUP BY ex.age
dbtng_example_form_add dbtng_example/dbtng_example.module Prepare a simple form to add an entry, with all the interesting fields.
dbtng_example_form_add_submit dbtng_example/dbtng_example.module Submit handler for 'add entry' form.
dbtng_example_form_update dbtng_example/dbtng_example.module Sample UI to update a record.
dbtng_example_form_update_callback dbtng_example/dbtng_example.module AJAX callback handler for the pid select.
dbtng_example_form_update_submit dbtng_example/dbtng_example.module Submit handler for 'update entry' form.
dbtng_example_grouping_list dbtng_example/dbtng_example.module This function groups the result set by the specified field and render a list of entries in the database
dbtng_example_help dbtng_example/dbtng_example.module Implements hook_help().
dbtng_example_install dbtng_example/dbtng_example.install Implements hook_install().
dbtng_example_list dbtng_example/dbtng_example.module Render a list of entries in the database.
dbtng_example_menu dbtng_example/dbtng_example.module Implements hook_menu().
dbtng_example_render_resultset_as_table dbtng_example/dbtng_example.module This function renders a resultset as table
dbtng_example_schema dbtng_example/dbtng_example.install Implements hook_schema().
dbtng_example_selective_list dbtng_example/dbtng_example.module Select only certain fields from the database

Classes

Namesort descending Location Description
DBTNGExampleUnitTestCase dbtng_example/dbtng_example.test Default test case for the dbtng_example module.