How to add a value to DBF

Asked

Viewed 105 times

0

To read the file . DBF works as follows:

I use a Tadoconnection, being the Connectionstring for:

Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=C:\_workspace\projects\DBFEditor\temp

To read the DBF file use a Tadoquery setting the SQL property for the query:

Select * from <arquivodbf>

So I have these columns in my dbf file.

INDICE  NOME    COR ESTILO  ESCALA
100     SAOJOAO      18      0,00

I need to change the name of the column INDEX FOR ID, for this I am doing as follows:

  while not ADOQuery1.Eof do
    begin
      Adoquery1.Edit;
      ADOQuery1.FieldByName('NOME').TEXT:= 'ID';
      Adoquery1.Post;
      ADOQuery1.Next;
    end;

however I get the following result, when opening my excel:

INDICE  NOME    COR ESTILO  ESCALA
 ID     SAOJOAO      18      0,00

how I expect the result:

 ID        NOME    COR ESTILO  ESCALA
 100     SAOJOAO        18      0,00
  • You do not change the name of the column, only change data on them. It should have some form, but in Delphi I do not know how to do, only externally.

  • But technically I’m setting a value, the INDEX field, it’s still a field

  • What I understand is that you want to change his name, this is a complex operation that Ado should not be able to do, at least not in a traditional way. Changing data is one thing, changing the column name is another. And this code is very confusing, to tell you the truth that it’s posted there doesn’t even really happen.

  • Exactly what I need, I’ll switch to a simpler way, but it’s working yes.

  • You indicate some way you might be doing this?

  • Just so I understand better, you have one. dbf and to connect to such database using the TADO component, traversing the records with Adoquery and saving the information in Excel?... if I’m wrong correct me.

  • Exactly, the issue @bigown said is that it is not possible to change the "header" that would be the first line of the file using DAO

  • In this case, the best way is to assemble a command alter, and run directly with Delphi. Just use the ExecuteDirect() of SQLConnection.

  • Just to complement this command you use ADOQuery1.FieldByName('NOME').TEXT:= 'ID'; is explicit that you are changing the contents of Field 'NAME' when you use . Text which is a property of the object.

  • @Jeffersonrudolf, from what I understand, wants to change Field’s name, not the content. Unless I’m mistaken.

  • Exactly, I want to change FIELD’s name

  • In order to change the fields, you have to go through them, do the following... before you go through the information you have to change the Fields and then feed the data. Try doing a for in Adoquery.FieldsDefs.Cout-1 and taking the first Field, doing so... Adoquery1.Fieldsdefs[0]. Name := 'ID'

  • @Victorzanella, he has to go through the query’s Fields in order to change

  • I think it would be easier for you to set up an external command, as @bigown thought, and this command you run by your Sqlconnection. Ex MySQLConnection.ExecuteDirect('ALTER TABLE table_name ALTER COLUMN column_name datatype')

  • @Victorzanella you could formulate a response with what commented? would help a lot

  • I didn’t formulate an answer, because I’m not sure. What happens is that at no point did you say you were using any database. I deduced this by the file extension. Dai I don’t know if you really use an Sqlconnection. If you confirm this for min, Edit your question by putting more code, I could formulate an answer.

  • I have improved the question, in case it is not yet clear can send here or call me by chat

  • @Guilhermelima, it would be interesting to pass the contents of Adoquery to a temporary table of Delphi, a very good component to manipulate this data would be the Tclientdataset, you create the Fields that you want and go through your Adoquery feeding the Clientdataset, after you have updated Clientdataset, just go through it and mount the data to be viewed in Excel.

Show 13 more comments

1 answer

0

I made an example of how I could do, follows below:

Declare no uses Data.DB, Datasnap.Dbclient

var
  oCDSDados: TClientDataSet;
  iPos: Integer;
begin
  oCDSDados := TClientDataSet.Create(nil);
try
  oCDSDados.FieldDefs.Add('ID',ftInteger);
  oCDSDados.FieldDefs.Add('NOME',ftString,50);
  oCDSDados.FieldDefs.Add('COR_ESTILO',ftInteger);
  oCDSDados.FieldDefs.Add('ESCALA',ftFloat);
  oCDSDados.CreateDataSet;

  ADOQuery1.First;
  while not (ADOQuery1.Eof) do
  begin
    oCDSDados.Append;
    oCDSDados.FieldByName('ID').AsInteger         := ADOQuery1.FieldByName('INDICE').AsInteger;
    oCDSDados.FieldByName('NOME').AsString        := ADOQuery1.FieldByName('NOME').AsString;
    oCDSDados.FieldByName('COR_ESTILO').AsInteger := ADOQuery1.FieldByName('COR_ESTILO').AsInteger;
    oCDSDados.FieldByName('ESCALA').AsFloat       := ADOQuery1.FieldByName('ESCALA').AsFloat;
    oCDSDados.Post;
    ADOQuery1.Next;
  end;

  //Primeira linha para incluir os fields no Excel.
  for iPos := 0 to oCDSDados.FieldDefs.Count-1 do
  begin
    // para pegar os fields do ClientDataSet
    oCDSDados.FieldDefs[iPos].Name;
  end;

  oCDSDados.First;
  while not (oCDSDados.Eof) do
  begin
    //
    // ESTRUTA PARA MONTAR O DADOS NO EXCEL
    //

   oCDSDados.Next;
 end;
finally
  if (oCDSDados.Active) then
    oCDSDados.Close;
  FreeAndNil(oCDSDados);
end;
end;

I hope I helped. Hugs!

  • Fieldsdefs.Count-1 Take That Change To Fielddefs.Count-1 Why He Didn’t Recognize Fields And He Returned: E2014 Statement expected, but Expression of type 'string' found

  • I’m sorry, it’s Fielddefs.Cout-1 I’ll fix it, you have to put a part of the code where he gives the problem

  • is in function oCDSDados.Fielddefs[ipos]. Name; E2014 Statement expected, but Expression of type 'string' found.. I put instead of name, Displayname.. there it rotates, but I don’t know if it’s the right way

  • You created the Excel file before the for and inside the for you have to play the clientdataset Fields in the first row of Excel, which Delphi such using?

  • I am using Delphi-xe8

  • was to work, such as feeding the variable with the return of oCDSDados.Fielddefs[ipos]. Name? but after you put the Displayname worked all?... or gave some more problem?

  • @Guilhermelima, managed to solve the problem?

Show 3 more comments

Browser other questions tagged

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