How do I change the path of my database when running the program?

Asked

Viewed 223 times

2

I’m starting to program with the Delphi XE8 and using Firebird 2.5.

I managed to implement my database and it’s working all right.

The problem I’m facing now is that as I change from computers to programming (home, college, etc), I have to change the path where my database file is located, compile and only then can I run the program.

I want to change my configuration TSQL Connection at runtime and not having to compile the program every time I switch from computer.

In case, create a Form with the program settings or, before the program initializes, request the database path.

In the image is highlighted the property I want to modify.

Em destaque a configuração que quero mudar em tempo de execução

I tried with this code:

   procedure TDMDados.ConexaoBeforeConnect(Sender: TObject);
   begin
       Conexao.Params:= 'Caminho do arquivo';
   end;

The problem is that, as I have already configured this connection, this parameter is already defined.

Some parameters of the connection:

   DriverName=Firebird
   DriverUnit=Data.DBXFirebird
   DriverPackageLoader=TDBXDynalinkDriverLoader,DbxCommonDriver220.bpl
   DriverAssemblyLoader=Borland.Data.TDBXDynalinkDriverLoader,Borland.Data.DbxCommonDriver,Version=22.0.0.0,Culture=neutral,PublicKeyToken=91d62ebb5b0d1b1b
   MetaDataPackageLoader=TDBXFirebirdMetaDataCommandFactory,DbxFirebirdDriver220.bpl
   MetaDataAssemblyLoader=Borland.Data.TDBXFirebirdMetaDataCommandFactory,Borland.Data.DbxFirebirdDriver,Version=22.0.0.0,Culture=neutral,PublicKeyToken=91d62ebb5b0d1b1b
   LibraryName=dbxfb.dll
   LibraryNameOsx=libsqlfb.dylib
   VendorLib=fbclient.dll
   VendorLibWin64=fbclient.dll
   VendorLibOsx=/Library/Frameworks/Firebird.framework/Firebird
   Database=C:\Users\Johnny\Dropbox\1.MESTRADO\10.DISSERTAÇÃO\PROGRAMA DELPHI\Banco de dados\bd.fdb
   User_Name=sysdba
   Password=masterkey
   Role=RoleName
   [...]

My intention is to change this connection parameter at runtime:

Database=C:\Users\Johnny\Dropbox\1.MESTRADO\10.DISSERTAÇÃO\PROGRAMA DELPHI\Banco de dados\bd.fdb

"- I still haven’t been able to try this because I don’t know how to start. I know to change the connection parameters before compiling".

  • Then, the program runs smoothly. I would like to change the connection configuration of my DB at runtime. I do not know if there is a way to modify the parameters of the connection before it is executed. I still haven’t been able to try this because I don’t know how to start. I know how to change the connection parameters before compiling....

  • I made some changes to the question and put the parameters of my connection. I did not find in the bibliographies that I read a native function of Delphi that modifies this parameter. As I can modify the Database parameter (as photo) directly by Object Inspector, I thought I had a command of the style " Connected.properties.Database ='path'. However I did not succeed in these cases.

  • 1

    Thanks for the help

  • Ready. I did what I could. Soon someone answers...

  • 1

    Thanks. It looks much better =D Now I have a little more base on how to format the questions around here

  • Then read the articles from help center which will already be a basis for understanding the overall functioning of the community. ;)

Show 1 more comment

1 answer

1

After catching much this is the solution I obtained, met my need in a satisfactory way.

Before the program creates the Form1 the user is asked to select the database file, so the program can get the file path. I created a function (Selectbdpath) to get the file path and then called the function and applied a variable which will be used in my Datamodule.

function SelectBDpath() : string ;
var
    selectedFile: string;
    dlg: TOpenDialog;
begin
    selectedFile := '';
    dlg := TOpenDialog.Create(nil);
    try
        dlg.InitialDir := GetCurrentDir;
        dlg.Filter :=   'Firebird DB Files|*.FDB';
        if dlg.Execute
            then
                SelectBDpath := dlg.FileName;
    finally
        dlg.Free;
   end;
end;
//Evento *OnCreate*
procedure TForm1.FormCreate(Sender: TObject);
    begin
        BDpath:=SelectBDpath();
        DMDados.Conexao.Open;
end;

In the sequence of my Datamodule ,before the connection occurs, "pull" the file path (Bdpath) of the Form1(for that it is necessary to put Unit corresponding in the you use of Datamodule) and apply in the connection parameters of my database, as code below:

procedure TDMDados.ConexaoBeforeConnect(Sender: TObject);
    begin
        Conexao.Params.Values['Database']:= BDpath;
    end;

This way every time I run the program it will ask to select the file from my database and apply in the connection parameters.

Browser other questions tagged

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