Board index » delphi » Re: ADO Database error regarding parameters

Re: ADO Database error regarding parameters


2005-07-07 04:22:23 AM
delphi179
Edward Diener writes:
Quote
Kevin Frevert writes:

>Ed,
>
>The code you posted has a colon in the SQL (11:16:24 AM). C++ or
>Delphi,
>it is interpreted as having parameters.


You are correct. I really missed that. However using a colon is the
traditional method of separating hours:minutes:seconds. How is one
supposed to deal with an Sql statement in which the data may have a
colon, even when parameters are being used ?
Never mind, I realized the answer, which is: turn off ParamCheck and
manually set parameters if necessary.
The error is that the VCL will automatically set up Parameters when it
sees the colon, so that when one has a colon in an sql statement one has
to stop the VCL from parsing it and set up one's own parameters instead.
I coild argue for a better parser, which is able to notice that colons
within data do not mean parameters, but I won't.
 
 

Re: ADO Database error regarding parameters

Edward Diener writes:
Quote
Bill Todd writes:

>First, remember that definition of a bug is something that does not
>work as designed. You are trying to define a bug as something that does
>not work the way you want it to.<g>
>
>Second, remember that very few SQL database servers support named
>parameters. Named parameters are implemented in the VCL components. If
>you look at the SQL that is sent to the server for most database
>servers you will see that the named parameters have been replaced by
>the standard question mark parameter placeholder. This means that to
>get the behavior you want the VCL components would have to be able to
>correctly parse every SQL dialect for every database server. The only
>alternative, and the only alternative that is practical, is to scan the
>SQL and assume that if you find a colon followd by one or more other
>characters you have a parmeter.
>
>I suspect that if you had to work with unnamed parameters for a couple
>of weeks you would beg to come back to what you have now. :)
>

Named parameters are the way to go. But suppose one has a colon among
the data to be inserted, which I now realize I did. How does one deal
with that in a named parameterized sql statment ?
Never mind, I realized that in the above case one must turn off
ParamCheck and set up Parameters internally.
 

Re: ADO Database error regarding parameters

"Edward Diener" <XXXX@XXXXX.COM>writes
Quote

The error is that the VCL will automatically set up Parameters when it
sees the colon, so that when one has a colon in an sql statement one has
to stop the VCL from parsing it and set up one's own parameters instead.

I coild argue for a better parser, which is able to notice that colons
within data do not mean parameters, but I won't.
I don't see that as an error in the VCL, that is just how it works (since
D1, IIRC). How would this 'better parser' do it 'better'? No argument,
just curious.
If your using parameters, turn ParamCheck on. If not, turn it off. I don't
know how much simpler it can get.
krf
 

Re: ADO Database error regarding parameters

The most common source of colons in SQL is time values. That is one of
the reasons that everyone recommends always using parameters for
date-time values instead of including the literal value in the SQL.
Turining off ParamCheck will, of course, also work.
--
Bill Todd (TeamB)
 

Re: ADO Database error regarding parameters

Bill Todd writes:
Quote
The most common source of colons in SQL is time values. That is one of
the reasons that everyone recommends always using parameters for
date-time values instead of including the literal value in the SQL.
Turining off ParamCheck will, of course, also work.

Thanks for the suggestion.
 

Re: ADO Database error regarding parameters

Kevin Frevert writes:
Quote
"Edward Diener" <XXXX@XXXXX.COM>writes
news:XXXX@XXXXX.COM...

>The error is that the VCL will automatically set up Parameters when it
>sees the colon, so that when one has a colon in an sql statement one has
>to stop the VCL from parsing it and set up one's own parameters instead.
>
>I coild argue for a better parser, which is able to notice that colons
>within data do not mean parameters, but I won't.


I don't see that as an error in the VCL, that is just how it works (since
D1, IIRC). How would this 'better parser' do it 'better'? No argument,
just curious.
It would be better if it realized that colons in any literal strings
were not parameters. A literal string being any sequence of characters
surrounded by single quotes in an sql statement.
Quote
If your using parameters, turn ParamCheck on. If not, turn it off. I don't
know how much simpler it can get.
If I am using parameters, but have colons in literal strings, turning
ParamCheck on will lead to errors. Of course if I manipulate my code to
check for colons in literal strings, and then replace the literal values
with parameters, I will be OK but that is much work to do to check all
my literal values for colons. So I do not think it is nearly as simple
as you think, and since the parser can't catch colons in literal
strings as non-parameters, I will turn ParamCheck off all the time and
always set up my own parameters, when I decide to use them, manually.
 

Re: ADO Database error regarding parameters

"Edward Diener" <XXXX@XXXXX.COM>writes
.
Quote

If I am using parameters, but have colons in literal strings, turning
ParamCheck on will lead to errors.
How is that? If your using parameters, it doesn't matter what is in the
literal string. Kind of the point of using parameters.
Example:
At design time, use a TADOCommand component and set the CommandText to..
Insert into TheTable
(TheText, TheDate)
Values
(:TheText, :TheDate)
Make sure the (Object Inspector->Parameters property editor) Parameters are
configured correctly.
In Delphi code..
{some datamodule}
private
public
function InsertTheText(const TheText :String; const ADate :TDateTime)
:Boolean;
end;
function TMyDatamodule.InsertTheText(const TheText :String; const
ADate:TDateTime) :Boolean;
begin
with qInsertText do
begin
try
Parameters.ParamByName('TheText').Value := TheText;
Parameters.ParamByName('TheDate;).Value := ADate;
Execute;
Result := True;
except
on E:Exception do
begin
Result := False;
end;
end
end;
end;
Now any Form or Datamodule can use this function..
{some on click event}
if MyDatamodule.InsertTheText(Memo1.Text, DatePicker1.Date) then
Showmessage('Yippee, it worked!')
else
ShowMessage('Oh {*word*99}, back to the newsgroups');
Quote
Of course if I manipulate my code to
check for colons in literal strings, and then replace the literal values
with parameters, I will be OK but that is much work to do to check all
my literal values for colons. So I do not think it is nearly as simple
as you think,
You are correct, that is why no one does that on purpose. Using VCL
parameters works, and works very well and why most C/S developers use them.
Good luck,
krf
 

Re: ADO Database error regarding parameters

"Edward Diener" <XXXX@XXXXX.COM>???????/???????? ?
???????? ?????????: news:42cc134f$XXXX@XXXXX.COM...
Quote
Vitali Kalinin writes:
>I would say that this is problem in yours coding style and apparently it
is
>not a bug.
>
>

Of course it can't be a bug that when ParamCheck is true and I have no
parameters in the CommandText which I Execute, I get intermittent ADO
errors but when ParamCheck is false under the same circumstances, I do
not get those intermittent ADO errors at all. It just must be my "coding
style". Right !

BTW this is C++ Builder 6 and not Delphi, so that it may not occur in
Delphi at all.
Right if you are constructing the whole SQL from a scratch and doesnt then
you should just use Execute method of AdoConnection. Using literal values
for not character data (and IMHO for character data also) is bad coding
style since it involves problems with special characters and regional
settings.