cannot make table through .install file into database

umeshsagar - September 16, 2008 - 00:32

i am new to drupal and trying to make table into drupal database. i have written create query in .install file and the book i am referring says that when we try to run drupal it will create table in drupal database automatically. can some one help me how can we do the database connection.....

My code is this.

<?php
// $Id$
function annotate_install()
{
drupal_set_message(t('Begin installion of anotate module.'));

switch ($GLOBALS['db_type'])
{
case 'mysql':
case 'mysqli':

db_query("CREATE TABLE annotations (
uid int NOT NULL default 0,
nid int NOT NULL default 0,
note longtext NOT NULL,
timestamp int NOT NULL default 0,
PRIMARY KEY (uid, nid)
);"
);

$success =TRUE;
break;

case 'pgsql':
db_query("CREATE TABLE annotations (
uid int NOT NULL default 0,
nid int NOT NULL default 0,
note longtext NOT NULL,
timestamp int NOT NULL default 0,
PRIMARY KEY (uid, nid)
);"
);
$success =TRUE;
break;

default:
drupal_set_message(t('Unsupported Database.'));
echo '1';
}
if ($success)
{
drupal_set_message(t('The module table installed successfully'));
echo '2';
}
else
{
drupal_set_message(t('Installation of annotation module was Unsuccessful'),'error');
echo '3';
}
}

does drupal create table automatically of we need to write some connection query in .module file for it.

Some things you have to do.

yfreeman - September 18, 2008 - 06:07

1. you need to create an modulename_uninstall() function - you can leave it blank if you want.

2. Disable the module. in the admin panel

3. click "uninstall" in the tab on the top of the modules page.

4. follow the steps. ie. click the button

5. re-enable the module and the code in the module_install() will run.

Note: You can bypass the whole database checking if you learn the Schema API which is extremely powerful API that abstract the database layer, I can't begin to explain what a joy it is.

 
 

Drupal is a registered trademark of Dries Buytaert.