Firedac: Getfieldnames without quotation marks

Asked

Viewed 458 times

3

I’m recovering the table fields Firebird and Mysql for Firedac Connection via the command line GetFieldNames, however some fields of the list are returned with quotes.

I have tried to insert the directive into the parameters MetaDefCatalog=MySql and solved nothing.

Below is the code I use to search the list of fields of a table:

Lista:=TStringList.Create;
FDConnection.GetFieldNames('','',Tabela,'',Lista);
if Lista.IndexOf('Campo') > 0 then
   //comandos para criar campo na tabela

The problem is that when the field is filled with quotes by Firedac (DBExpress wouldn’t do that) clause if asks to create the field that already exists and generates an error.

How do I fix it?

Thank you!!!

2 answers

2

Fields are returned with Quotes due to database normalization. Here a little different approach: Documentation.

Alist is a Tstrings Scendant that receives the field Names. Any existing strings are Deleted from the list before Getfieldnames adds the Names of all the Fields in Atablename.

The field Names are normalized-Enclosed in quotation Marks, if that is required-otherwise converted to default Dictionary case.

There are several functions that remove unwanted characters, this can help you, after getting names and before inserting in the list, pass the result by the function:

function RemoveEspeciais(Texto: Ttring): Ttring; stdcall;
{Função que serve para nao aceitar caracteres especiais tipo !@#$%^&*()}
const
  NaoChar = '~`!@#$%^&*()_-+=|\<>,.?/æ';
var
  i: Integer;
begin
  for i := 1 to Length(Texto) do
    if Pos(Texto[i], NaoChar) <> 0 then
    else
    Result := Result + Texto[i];
end;

If you can’t add the Single Quote, add the function constant #39 that equals it!

EDIT

Changing the approach, now knowing that the project is great!

Declare in the uses of your project: FireDAC.VCLUI.Wait

procedure TfrmPrincipal.btnTesteClick(Sender: TObject);
var
  vNomeCampos : TStringList;
begin
  vNomeCampos := TStringList.Create;
  FDConnection.GetFieldNames('','','nome_tabela','',vNomeCampos);//nome_tabela entre Aspas!
  ShowMessage(vNomeCampos.Text);
end;
  • Junior, thank you for your answer. But what I hope is a way to normalize the problem in Fdconnection, because I have a huge version update file that uses this same Fdconnection.Getfieldnames function to find out if the field does not exist create it in the table.

  • @prmas, Following change in response, detail, the Trial version here is Tool = RAD Studio XE7 Firedac = 11.0.1 (Build 73709)

2

guy I have a data migration system developed and use getfildName to pick up the fields of a particular tambela as follows.

 fdconnection.GetFieldNames(NomeDoBanco,'',Tabela,'',StringList);

And from what I’ve used in the system, he never took it or Firedac put quotes. but try to carry out the following test the index by which I searched serves to know which index corresponds to the string and it returns a value of type int. However the string list starts at 0 and goes to the end, maybe its field is at position 0, and you are validating if it is greater than 0. If this is it put an Xyz field to see the return, I think it will be -1.

hope I’ve helped

Browser other questions tagged

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