Solved

Log the Sql of TAdoQuery

Posted on 2007-04-02
5
239 Views
Last Modified: 2010-04-05
Hi there :-)

I got a TAdoQuery and I want to log the sql statement's that it process's.
However I also work with paramaters, like
SELECT * FROM this WHERE that=:S1

So is there a way to get the fully formed sql statement? And not just
SELECT * FROM this WHERE that=:S1
0
Comment
Question by:wildzero
  • 3
  • 2
5 Comments
 
LVL 21

Expert Comment

by:ziolko
ID: 18838932
if you want to log your sql statements simplest way is any time you
call ADOQuery.SQL.Add(sql_stat) add sql_stat also to some stringlist
then you can save your stringlist to file or dump to clipboard or anythink you want.
saving params might be possible (not tested) in this way
ADOQuery.SQL.Add(sql_stat)
mystringlist.add(ADOQuery.SQL.Text)
but there's little trick for example if param is blob you might have problems
with saving param value.

ziolko.
0
 
LVL 10

Author Comment

by:wildzero
ID: 18839085
Probly have to write my own function then to get the params.....

saving the
SELECT * FROM this WHERE that=:S1
isn't a problem - thats easy to log.
But when I do log it, I want the params included....

0
 
LVL 21

Accepted Solution

by:
ziolko earned 125 total points
ID: 18839153
you can loop thru Params collection read values, convert them to string and then use StringReplace() on sql command

not tested sample:
procedure TForm1.Button1Click(Sender: TObject);
var prm: string;
    sql: string;
    sql_noparams: string;
begin
  ADOQuery1.SQL.Add(sql);
  sql_noparams := sql;
  for cnt := 0 to ADOQuery1.Parameters.Count - 1 do begin
    prm := VarToStr(ADOQuery1.Parameters[cnt].Value);
    sql_noparams := StringReplace(sql_noparams, ADOQuery1.Parameters[cnt].Name, prm, [rfReplaceAll, rfIgnoreCase]);
  end;
end;


ziolko.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18839191
of course (missed ':'):

sql_noparams := StringReplace(sql_noparams, ':' + ADOQuery1.Parameters[cnt].Name, prm, [rfReplaceAll, rfIgnoreCase]);

ziolko.
0
 
LVL 10

Author Comment

by:wildzero
ID: 18839740
Yea that might be the way to do it...
Will stick that in and see how it runs.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question