I need to setup/run a fdconnection and fdquery, in runtime. But how?
I am trying this in my code:
var
LocalConnection: TFDConnection;
LicencasTable: TFDQuery;
FDPhysMySQLDriverLink1: TFDPhysMySQLDriverLink;
FDGUIxWaitCursor1: TFDGUIxWaitCursor;
oParams: TStrings;
begin
LocalConnection := TFDConnection.Create(Self);
FDPhysMySQLDriverLink1 := TFDPhysMySQLDriverLink.Create(Self);
FDGUIxWaitCursor1 := TFDGUIxWaitCursor.Create(Self);
FDPhysMySQLDriverLink1.VendorLib := ExtractFilePath(Application.exename)+'libmysql.dll';
try
if Variaveis.hostbd <> '' then begin
oParams := TStringList.Create;
oParams.Add('Server=' + Variaveis.hostbd);
oParams.Add('Database='+Variaveis.databasebd);
oParams.Add('User_Name='+Variaveis.usuariobd);
oParams.Add('Password='+Variaveis.password);
oParams.Add('DriverID=MySQL');
LocalConnection.Params.UserName := Variaveis.usuariobd;
LocalConnection.Params.Password := Variaveis.password;
FDManager.AddConnectionDef('bloqueio', 'MySQL', oParams);
LocalConnection.DriverName := 'MySQL';
LocalConnection.LoginPrompt := false;
LocalConnection.Connected := true;
if LocalConnection.Connected then
// Busca o registro do sistema no bd
LicencasTable := TFDQuery.Create(Self);
LicencasTable.Connection := LocalConnection;
ShowMessage(Pchar(LicencasTable.Connection));
LicencasTable.Close;
LicencasTable.SQL.Clear;
ShowMessage('Before open');
LicencasTable.Open('select * from licencas');
ShowMessage('After open');
LicencasTable.Active := true;
if LicencasTable.Active then begin
ShowMessage('Active');
end;
if not LicencasTable.Active then begin
ShowMessage('This is not Active');
end;
end;
except
oParams.Free;
end;
Everything works correctly, until reach the "LicencasTable.Open('select * from licencas');"
The query.open simply dont works, and gives me no error too. Why not open, and not show me a error?
What am I forgetting to do?
Your code is incorrect in a couple of ways. First, you don't have to close it or clear the SQL on a newly created query. Second, you don't typically use the Open
method to set up the SQL statement. Third, you're using a try..except
to free resources, which is incorrect; if no exception occurs, you have a memory leak. You should be using a try..finally
instead.
try
if Variaveis.hostbd <> '' then begin
oParams := TStringList.Create;
oParams.Add('Server=' + Variaveis.hostbd);
oParams.Add('Database='+Variaveis.databasebd);
oParams.Add('User_Name='+Variaveis.usuariobd);
oParams.Add('Password='+Variaveis.password);
oParams.Add('DriverID=MySQL');
LocalConnection.Params.UserName := Variaveis.usuariobd;
LocalConnection.Params.Password := Variaveis.password;
FDManager.AddConnectionDef('bloqueio', 'MySQL', oParams);
LocalConnection.DriverName := 'MySQL';
LocalConnection.LoginPrompt := false;
LocalConnection.Connected := true;
if LocalConnection.Connected then
begin
// Busca o registro do sistema no bd
LicencasTable := TFDQuery.Create(Self);
LicencasTable.Connection := LocalConnection;
ShowMessage(Pchar(LicencasTable.Connection));
ShowMessage('Before open');
LicencasTable.SQL.Text := 'select * from licencas';
LicencasTable.Active := true;
if LicencasTable.Active then begin
ShowMessage('Active');
end;
if not LicencasTable.Active then begin
ShowMessage('This is not Active');
end;
end;
finally
oParams.Free;
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With