1

I need to create a Firebird Database programmatically using DBExpress. I have done this for SQL server, by first connecting to Master, then passing in the script for Create to a query, but with Firebird I have a little chicken and egg problem.

0

2 Answers 2

3

I got a good tip from a collegue that created some code for the Freepascal project. It doesn't use DB express, but according to him it is the only way to create a database with code. This code is based on the InterBase manual, and uses a call from the gdslib / fbclient dll:

procedure TIBConnection.CreateDB;

var ASQLDatabaseHandle,
    ASQLTransactionHandle : pointer;
    CreateSQL : String;
    pagesize : String;
begin
  CheckDisConnected;
  {$IfDef LinkDynamically}
    InitialiseIBase60;
  {$EndIf}
  ASQLDatabaseHandle := nil;
  ASQLTransactionHandle := nil;
  CreateSQL := 'CREATE DATABASE ';
  if HostName <> '' then
    CreateSQL := CreateSQL + ''''+ HostName+':'+DatabaseName + ''''
  else
    CreateSQL := CreateSQL + '''' + DatabaseName + '''';

  if UserName <> '' then
    CreateSQL := CreateSQL + ' USER ''' + Username + '''';
  if Password <> '' then
    CreateSQL := CreateSQL + ' PASSWORD ''' + Password + '''';
  pagesize := params.Values['PAGE_SIZE'];
  if pagesize <> '' then
    CreateSQL := CreateSQL + ' PAGE_SIZE '+pagesize;

  if isc_dsql_execute_immediate(@FStatus[0],@ASQLDatabaseHandle,@ASQLTransactionHandle,length(CreateSQL),@CreateSQL[1],Dialect,nil) <> 0 then
    CheckError('CreateDB', FStatus);

  if isc_detach_database(@FStatus[0], @ASQLDatabaseHandle) <> 0 then
    CheckError('CreateDB', FStatus);

  {$IfDef LinkDynamically}
    ReleaseIBase60;
  {$EndIf}
end;

The trick is the isc_dsql_execute_immediate function. I hope this code helps you. Here are the links to the Freepascal source files where this code comes from:

Unit containing CreateDB function

Unit containing API call isc_dsql_execute_immediate

Sign up to request clarification or add additional context in comments.

1 Comment

Installation directory of Firebird contains some model examples. Check following directory on your computer: C:\Program Files\Firebird\Firebird_2_5\examples\api Method prototypes can be found in directory: C:\Program Files\Firebird\Firebird_2_5\include
1

Run a script in isql

isql -i createDB.sql

The file createDB.sql includes the command to create the database as shown below.

SET SQL DIALECT 3;
CREATE DATABASE 'C:\DATABASE\DB.FDB'
USER 'SYSDBA' PASSWORD 'masterkey'
PAGE_SIZE 4096
DEFAULT CHARACTER SET WIN1252;
QUIT;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.