Board index » delphi » How to create a new database in Delphi?

How to create a new database in Delphi?

 Hi everybody,

 I need to create a new Interbase database using Delphi. But my problem is
that
before I must connect to an existent database to do anything using SQL. How
can I create a new
database, using the Delphi's components, just connecting to Interbase,
without to use
an existent database to do it? The same way I do when I'm in the Interbase
Console, when I create a new database... Is there some component or way to
do it?

Fabrcio

 

Re:How to create a new database in Delphi?


"Fabrcio Wendt" <fwe...@kopp.com.br> schreef in bericht
news:3ba72fdc_1@dnews...

Quote
> Hi everybody,

>  I need to create a new Interbase database using Delphi. But my problem is
> that
> before I must connect to an existent database to do anything using SQL.
How
> can I create a new
> database, using the Delphi's components, just connecting to Interbase,
> without to use
> an existent database to do it? The same way I do when I'm in the Interbase
> Console, when I create a new database... Is there some component or way to
> do it?

Use TIBDatabase to do it.
Note ! param for user is not "user_name" but "user"
I hope this helpes you.

Example :

with TIBDatabase.Create(nil) do
try
  with Params do
  begin
    Add('user ''SYSDBA''');
    Add('password ''masterkey''');
    Add('PAGE_SIZE 4096');
  end;
  DatabaseName := 'server:C\test.gdb';
  CreateDatabase;
finally
  Free;
end;

Greetings,
Arno Brinkman.

Quote

> Fabrcio

Re:How to create a new database in Delphi?


"Fabrcio Wendt" <fwe...@kopp.com.br> schreef in bericht
news:3ba72fdc_1@dnews...

Quote
> Hi everybody,

>  I need to create a new Interbase database using Delphi. But my problem is
> that
> before I must connect to an existent database to do anything using SQL.
How
> can I create a new
> database, using the Delphi's components, just connecting to Interbase,
> without to use
> an existent database to do it? The same way I do when I'm in the Interbase
> Console, when I create a new database... Is there some component or way to
> do it?

You can do it with TIBDatabase.
Note ! Param user must be "user" and not "user_name" !
I hope this helpes you.

Example :

with TIBDatabase.Create(nil) do
try
  with Params do
  begin
    Add('user ''SYSDBA''');
    Add('password ''masterkey''');
    Add('PAGE_SIZE 4096');
  end;
  DatabaseName := 'server:C:\test.gdb';
  CreateDatabase;
finally
  Free;
end;

Greetings,
Arno Brinkman.

Re:How to create a new database in Delphi?


Below is a working example I worked out with some help from this
group...

Procedure TDMPhrases.NewDB(FName:String);
Begin
  With DBPhrases do begin
    Connected := FALSE;
    Databasename := FName;
    Params.Clear;
    Params.Add('USER "SYSDBA"');
    Params.Add('PASSWORD "masterkey"');
    Params.Add('PAGE_SIZE 4096');
    LoginPrompt := FALSE;
    SQLDialect := 3;
    CreateDatabase;
    End;
  TRNPhrases.StartTransaction;
  With PhrasesSQL do begin
    Close;
    SQL.Clear;
    SQL.Add('CREATE DOMAIN "PKDOMAIN" AS NUMERIC(8, 0);');
    ExecQuery;
    Close;
  TRY
    SQL.Clear;
    SQL.Add('CREATE TABLE "PHRASES" (');
    SQL.Add('  "PKEY" "PKDOMAIN" NOT NULL,');
    SQL.Add('  "PHRASE"       CHAR(128),');
    SQL.Add('  "MEMO" BLOB SUB_TYPE TEXT SEGMENT SIZE 80,');
    SQL.Add(' PRIMARY KEY ("PKEY") );');
    ExecQuery;
    Close;
    SQL.Clear;
    SQL.Add('CREATE TABLE "PHRASEXREF" (');
    SQL.Add('  "LSIDE"        "PKDOMAIN" NOT NULL,');
    SQL.Add('  "RSIDE"        "PKDOMAIN" NOT NULL,');
    SQL.Add('  "RELTYPE"      INTEGER NOT NULL,');
    SQL.Add(' PRIMARY KEY ("LSIDE", "RSIDE", "RELTYPE") );');
    ExecQuery;
    Close;
    SQL.Clear;
    SQL.Add('CREATE TABLE "RELATIONS" (');
    SQL.Add('  "RELNAME"      CHAR(48),');
    SQL.Add('  "RELTYPE"      INTEGER NOT NULL,');
    SQL.Add('CONSTRAINT "PK_RELATIONS" PRIMARY KEY ("RELTYPE") );');
    ExecQuery;
    Close;
    SQL.Clear;
    SQL.Add('CREATE UNIQUE INDEX "IXPHRASE" ON "PHRASES"("PHRASE");');
    ExecQuery;
    Close;
    SQL.Clear;
    SQL.Add('CREATE INDEX "IXLEFT" ON "PHRASEXREF"("LSIDE");');
    ExecQuery;
    Close;
    SQL.Clear;
    SQL.Add('CREATE INDEX "IXRELTYPE" ON "PHRASEXREF"("RELTYPE");');
    ExecQuery;
    Close;
    SQL.Clear;
    SQL.Add('CREATE INDEX "IXRIGHT" ON "PHRASEXREF"("RSIDE");');
    ExecQuery;
    Close;
    SQL.Clear;
    SQL.Add('ALTER TABLE "PHRASEXREF" ADD FOREIGN KEY ("LSIDE")
REFERENCES PHRASES ("PKEY") ON DELETE CASCADE;');
    ExecQuery;
    Close;
    SQL.Clear;
    SQL.Add('ALTER TABLE "PHRASEXREF" ADD FOREIGN KEY ("RSIDE")
REFERENCES PHRASES ("PKEY") ON DELETE CASCADE;');
    ExecQuery;
    Close;
    SQL.Clear;
    SQL.Add('ALTER TABLE "PHRASEXREF" ADD FOREIGN KEY ("RELTYPE")
REFERENCES RELATIONS ("RELTYPE") ON DELETE CASCADE;');
    ExecQuery;
    Close;
    SQL.Clear;
    SQL.Add('CREATE GENERATOR "GENRELNUM";');
    ExecQuery;
    Close;
    SQL.Clear;
    SQL.Add('CREATE GENERATOR "PKGENERATOR";');
    ExecQuery;
    Close;
    SQL.Clear;
    SQL.Add('CREATE TRIGGER "NEWPHRASETRIGGER" FOR "PHRASES"');
    SQL.Add('ACTIVE BEFORE INSERT POSITION 0');
    SQL.Add('AS');
    SQL.Add('BEGIN');
    SQL.Add('NEW.PKey = GEN_ID(PKGenerator, 1);');
    SQL.Add('END;');
    ExecQuery;
    Close;
    SQL.Clear;
    SQL.Add('CREATE TRIGGER "NEWRELTRIGGER" FOR "RELATIONS"');
    SQL.Add('ACTIVE BEFORE INSERT POSITION 0');
    SQL.Add('AS');
    SQL.Add('BEGIN');
    SQL.Add('NEW.RelType = GEN_ID(GENRELNUM, 1);');
    SQL.Add('END;');
    ExecQuery;
    Close;
  EXCEPT on Exception do TRNPhrases.RollBack
  END
    End;
  CommitWork;
End;

Quote
"Fabrcio Wendt" wrote:

>  Hi everybody,

>  I need to create a new Interbase database using Delphi. But my problem is
> that
> before I must connect to an existent database to do anything using SQL. How
> can I create a new
> database, using the Delphi's components, just connecting to Interbase,
> without to use
> an existent database to do it? The same way I do when I'm in the Interbase
> Console, when I create a new database... Is there some component or way to
> do it?

> Fabrcio

Other Threads