Firedac: GetFieldNames without quotation marks

3

I'm retrieving the Firebird and Mysql tables by Firedac Connection through the command line GetFieldNames , however some fields in the list are returned with quotation marks.

I have tried to insert the MetaDefCatalog=MySql directive into the parameters and it did not solve anything.

Below is the code I use to fetch the list of fields from 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 quoted by Firedac ( DBExpress did not do this) the if clause asks to create the field that already exists and generates an error. / p>

How do I resolve this?

Thank you !!!

    
asked by anonymous 15.12.2015 / 14:50

2 answers

2

Fields are returned with quotation marks because of database normalization. Here's a little different approach: Documentation.

  

AList is a TStrings descendant 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 the unwanted characters, this can help you, after getting the 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 not add the single quote, add in the function constant the # 39 that is the same!

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;
    
15.12.2015 / 16:25
2

Dude I have a data migration system developed and I use getfildName to pick up the fields from a given dump as follows.

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

And from what I have already used the system he never picked up or FireDac put quotes. but try the following test the indexof I searched for is what index to match the string and it returns a value of type int. But the string list starts at 0 and goes to the end, maybe your field is at position 0, and you are validating if it is greater than 0. If so, put a xyz field to see the return, I think it will be -1.

I hope to have helped

    
30.12.2015 / 14:54