Solved

Log the Sql of TAdoQuery

Posted on 2007-04-02
5
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

752 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