t e m p o r a l 
 d o o r w a y 

Creating A Database at Runtime

 

Creating a database at run time is hard, but not that hard.

It won't work if you don't have the drivers installed. Let's start with creating a dBase database.

Create the directory for the database, using

#include <dir.h>

mkdir(Path); // Make sure all the directories above exist.

Create the alias for the database (dBase in this case)

try
{
   Session->AddStandardAlias
   (
      Alias,
      Path + AnsiString("\\DBase"),
      "DBASE"
   );
}
catch (EDatabaseError &DatabaseError)
{
   Application->MessageBox
   (
      DatabaseError.Message.c_str(),
      "Error in alias creation",
      0
   );
};


try
{
   Session->SaveConfigFile();
}
catch (EDatabaseError &DatabaseError)
{
   Application->MessageBox
   (
      DatabaseError.Message.c_str(),
      "Error saving BDE configuration changes",
      0
   );
};

Next create the table using a Tquery:

CreateTable->SQL->Close();
CreateTable->SQL->DatabaseName = Alias;
CreateTable->SQL->Clear();
CreateTable->SQL->Add("CREATE TABLE Test (ID INTEGER,FIELD_1 CHAR(30))");
CreateTable->SQL->ExecSQL();

Now the only difference for an ODBC table is the replacement of the AddStandardAlias call with

TStringList *DriverInfo = new TStringList;
DriverInfo->Add("USER NAME=DefaultUser"); // Whatever would go with the Alias in BDE Config
Session->AddAlias(Alias,AnsiString("ODBC_ACCESS"),DriverInfo); // Assuming you created the driver in BDE as ODBC_ACCESS
delete DriverInfo;

It's somewhat harder if the BDE alias for the driver hasn't been created.

Copyright © 2004 by Mark Cashman (unless otherwise indicated), All Rights Reserved