Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Possible Duplicate:
Adding content to a database table during module install

rewrite: I've got a question about Db tables.

I'm building a custom module, and used hook_install and hook_schema to set up tables. What I need to do is - at the same time my table is created to insert some base information into that new table

For example:

function MyModule_install() {
drupal_install_schema('MyModuleTable');
INSERT INTO `MyModuleTable` (`age`, `savingvalues`) VALUES ('33', '18.01');
}

How do I create the table and write the 40+ inserts at the same time? I'm in D6 and am hoping for an example. Thanks!

share|improve this question
What do you mean by, "with initial data" in the title? – kiamlaluno Feb 1 at 17:36
sorry if that was vague, instead of a table to receive the info from users, i need to start with a set of values. it's that initial set of values I need to load when the module is enabled – Stephanie Feb 1 at 18:04
If you change your question, then please don't do it with an addendum at the end, instead, re-write the original question, and I would vote to re-open. – Letharion Feb 5 at 13:51
Thanks for clarifying for me, I'm still pretty new here. I'll re-write the question today. – Stephanie Feb 5 at 19:02

marked as duplicate by Jarrod Dixon Feb 1 at 23:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

This is taken from the cck module. first you would define the schema you want to call in the hook_install funtion

/**
 * Implementation of hook_install().
 */
function content_install() {

  drupal_install_schema('content');
}

The you would define the schema as follows this is just the first schema defined in the cck module the entire function is a bit large

/**
 * Implementation of hook_schema.
 */
function content_schema() {

  // Static (meta) tables.

  $schema['content_node_field'] = array(
    'fields' => array(
      'field_name'      => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
      'type'            => array('type' => 'varchar', 'length' => 127, 'not null' => TRUE, 'default' => ''),
      'global_settings' => array('type' => 'text', 'size' => 'medium', 'not null' => TRUE, 'serialize' => TRUE),
      'required'        => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0),
      'multiple'        => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0),
      'db_storage'      => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 1),
      'module'          => array('type' => 'varchar', 'length' => 127, 'not null' => TRUE, 'default' => ''),
      'db_columns'      => array('type' => 'text', 'size' => 'medium', 'not null' => TRUE, 'serialize' => TRUE),
      'active'          => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0),
      'locked'          => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0),
    ),
    'primary key' => array('field_name'),
  );
return $schema;
}

You can take a look at the contributed modules and you should be able to get a better idea.

Reference

share|improve this answer
Thank you, I do know how to add the table to the DB, what I need is to get rows of content into the DB at the same time. THanks for the reply. – Stephanie Feb 4 at 16:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.