docs.roxen.comView this page in a printer friendly mode
DocsRoxen2.1TutorialsDatabase Tutorial
Copyright © 2001, Roxen Internet Software
Suggestions, comments & compliments
manuals@roxen.com
 DEMO  DOCS  PIKE
 COMMUNITY  DOWNLOAD
www.roxen.com

   

Installing MySQL
Privileges
Building a Sample Database
The query() function
The big_query() function
Quoting
SQL Syntax
Conditions
Sorting
Limiting
Functions
Features Missing from MySQL
Insertion Syntax
The tablify Container
The Business Graphics Module
The emit and sqlquery Tags
Database Creation
Creating Tables
Indices
Dropping

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).