there. I am trying to search for a string contained in any column of a sql table by adding all the Fieldnames to the WHERE clause using a for loop. Also I use parameters to protect against SQL injection. But when I run I get an error like this:
Unspecified error
How can this be fixed and what is the problem (Not necessarily in that order). Here is my code. I am running Delphi 7
procedure TfrmView.edtSearchChange(Sender: TObject);
var
i, i2: integer;
obj: TEdit;
QueryText: string;
begin
obj:= Sender as TEdit;
with dmInfo do
begin
qryInfo.SQL.Clear;
qryInfo.SQL.Add('SELECT * FROM ' + tableName);
qryInfo.Open;
tblInfo.SQL.Clear;
tblInfo.SQL.Add('SELECT * FROM ' + tableName);
tblInfo.SQL.Add('WHERE (' + qryInfo.Fields[0].FieldName + ' LIKE :SQuery0)');
QueryText:= '%' + obj.Text + '%';
tblInfo.Parameters.ParamByName('SQuery0').Value:= QueryText;
ShowMessage(QueryText);
ShowMessage(tblInfo.Parameters.ParamByName('SQuery0').Value);
for i:= 1 to qryInfo.FieldCount - 1 do
begin
tblInfo.SQL.Add(' OR (' + qryInfo.Fields[i].FieldName + ' LIKE :SQuery' + IntToStr(i) + ')');
tblInfo.Parameters.ParamByName('SQuery' + IntToStr(i)).Value:= '%' + obj.Text + '%';
end;
tblInfo.Open;
end;
The whole code makes no sense.
you are using a variable, which suggests that this event is also connected to other TEdits.
obj: TEdit;
obj:= Sender as TEdit;
You clear the SQL on an open table.
It is confusing, misleading and unnecessary.
First of all remove the code out of the onChange event
What you want to do with this code
to search for values on all fields from the
tblInfo
can be done without the first tabelqryInfo
you do not need to increment the params.
Do not create the params all the time from
SQuery1
to maybeSQuery100
if you only use one param (the search value is always the same)
You can set all params with a single use
tblInfo.Parameters.ParamByName()
before thetblInfo.Open
but NOT in the loop.
This will replace all params
:SQuery
all at once with the valueSQL.Text :
Maximum Length Of An SQL Statement
Is different from database to database
How many OR clauses can I use in a single WHERE condition in MySql query?
I know there was a limit in the past. But now the experts knowledge is different
only one of the opinions