Reading Text from File

ReaperUser101 0 Tallied Votes 218 Views Share

Hi guys

The just of what i whant to do is read lines of text from a file until it reaches
a delimiter and then execute that text in a query and repeat the process till
there is no more text. I am using Delphi 6(don't have a choice) and the text is SQL in a .txt file

The code I provided does not work because it doesn't use the delimiter(defaults to whitespace)
and a stringlist is not compatable.

If anyone has any advice let me know
Great thanks in advance
R.

sqlRead := TStringList.Create;
  sqlRead.Delimiter := '|';
  sqlRead.LoadFromFile('C:\Users\charll\Desktop\tables.txt');

  for i := 0 to sqlRead.Count-1 do
  begin
  ShowMessage((sqlRead[i]));
  CDSADOQuery1.SQL.Clear;
  CDSADOQuery1.SQL.Text := sqlRead;
  CDSADOQuery1.ExecSQL;
  CDSADOQuery1.Active := True;
  end;
pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

Set StrictDelimiter to True to disable the space delimiter.

ReaperUser101 0 Newbie Poster

I don't think I have that setting, I'm using delphi 6 and read in another source that it doesn't exist yet in Delphi 6.

pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

Found a comment that in older versions the space is always used as delimiter, so perhaps this is the case here. You may have to parse the file yourself, to get it right. If you are sure that the pipe character is not used in your data, you can use Pos(), Copy() and Delete() to get your queries.

ReaperUser101 0 Newbie Poster

Thanks for the replies but managed it with the next code although it is faulty due toe the fact that if the one table exists it doesn't create the next one but works at least.

qryMain.SQL.LoadFromFile(lsLocation+'\tables.txt');
      qryMain.ExecSQL;
      qryMain.SQL.Clear;
ReaperUser101 0 Newbie Poster

lsLocation is a string that gets the current directory (The location of the .exe file) where the text file is

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.