Createdatabase Delphi VCL

Asked

Viewed 304 times

3

How to create the database automatically if it does not exist, same in app from Delphi Android, but I want to do it with Delphi VCL. I want my application in the first run to automatically create the database where I will run the scripts to generate the tables.

I’m trying following code:

if NOT FileExists(Trim(sPath)) then
  conn.Params.Values['CreateDatabase']  := 'True'
Else
  conn.Params.Values['CreateDataBase']  := 'False';

I’m using Delphi XE10 with Firebird and connect with FDConnection.

2 answers

4

While this prospecting in Delphi (trial and error) and Google managed to solve, besides assigning 'True' to Createdatabase also assigns the other params ai yes it worked, Follow example

    procedure Tdm.connBeforeConnect(Sender: TObject);
    var sPath: string;
    begin
       sPath := gsAppPath +'DB\MeuDB.fdb' ;
       FDConnection1.Params.Values['CreateDatabase'] := BoolToStr(not FileExists(Trim(sPath)),True);
       FDConnection1.Params.Values['Database']     := Trim(sPath);
       FDConnection1.Params.Values['DriverID']     := 'FB';
       FDConnection1.Params.Values['User_Name']    := 'SYSDBA';
       FDConnection1.Params.Values['Password']     := 'masterkey';
       FDConnection1.Params.Values['CharacterSet'] := 'WIN1252';
       FDConnection1.Params.Values['Dialect']      := '3';
    END;

Thank You All.

1

To complement the above answer Places a component of TFDQuery

FDQueryclose; 
FDQueryclose.sql.clear; 
FDQueryclose.sql.add (' create table teste('                ); 
FDQueryclose.sql.add ('    n_fields1    integer default 0, '); 
FDQueryclose.sql.add ('    n_fields2    integer default 0, '); 
FDQueryclose.sql.add ('    n_fields3    varchar(50))       '); 
FDQueryclose.ExecSQL;

So you Create the tables.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.