954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Reading Text from File

0
By ReaperUser101 on Mar 3rd, 2011 1:35 pm

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;

Set StrictDelimiter to True to disable the space delimiter.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

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.

ReaperUser101
Newbie Poster
15 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

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.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

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
Newbie Poster
15 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

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

ReaperUser101
Newbie Poster
15 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You