docs.roxen.comBack to normal mode
DocsRoxenWebServer 3.4TutorialsDatabase Tutorial
Copyright © 2006, Roxen Internet Software
Suggestions, comments & compliments
manuals@roxen.com

Database Creation

Database creation is not a part of the SQL standard, and the details are very much server-specific. The Pike SQL interface, however, offers two functions as part of the Sql.sql object that can serve for this purpose.

Create a "foo" database:

void create_new_database(string dbname) 
{
  mixed error;
  object db = Sql.sql("mysql://admin:password@localhost/");
  error = catch {
    db->create_db("newdb");
  };
  if(error) 
  {
    werror("Error: "+db->error()+"\n");
    return;
  }
}

Delete the "foo" database:

void delete_database(string dbname)
{
  object db = Sql.sql("mysql://admin:password@localhost/");
  db->drop_db("newdb");
}

Of course the catch {} clause in the first example is overkill here, these operations are really REALLY meant to be used interactively, and so a stack backtrace can be very descriptive and useful.

Most servers provide an SQL syntax to perform this operation. In some cases creating a database is so expensive that an external app is used to perform the operation. When your server supports it via SQL, using SQL is advised. This functions are provided mostly for MiniSQL compatibility (MiniSQL doesn't provide an SQL syntax to create a database).