CreateDatabase Delphi VCL

3

How to create the database automatically if it does not exist, same in the app of Delphi Android, but I want to do with Delphi VCL. I want my first run application to automatically create the database where I'm going to run 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 connected with FDConnection .

    
asked by anonymous 21.09.2017 / 16:03

2 answers

4

While doing this in Delphi (trial and error) and Google I managed to solve, in addition to assigning 'True' to the CreateDatabase also assign the other params, yes it worked,

    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;

Thanks to everyone.

    
21.09.2017 / 17:32
1

To complement the above answer Puts 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.

    
04.10.2017 / 13:54