Solved

Assistance needed with SQL Syntax

Posted on 2007-11-21
22
167 Views
Last Modified: 2010-04-05
Hi all,

I need some assistance with some code that I would like to expand on:

Here is the code:

if sModels.Text<>'ANY' then
    Begin
      ADOQuery1.SQL.Add(
        'Select * from motors where (Make) = '
        + Quotedstr(ComboBox1.text)
        + ' AND ((Model) LIKE "%'  
        + ComboBox2.text            //Fiesta
        + '%")'                     // %")
        );
     
    end;

I need to add

AND (Price) <= PriceComboBox.text

also PriceComboBox.text is a String ....Do I need to Add StrToInt for it to work???

Hope you can help

ST3VO
0
Comment
Question by:ST3VO
  • 9
  • 6
  • 5
  • +1
22 Comments
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20326780
Try this. And because you are inserting SQL as a string, you want to send it through as a string not an integer anyway.
if sModels.Text<>'ANY' then

    Begin

      ADOQuery1.SQL.Add(

        'Select * from motors where (Make) = '

        + Quotedstr(ComboBox1.text) 

        + ' AND ((Model) LIKE "%'  

        + ComboBox2.text            //Fiesta

        + '%")'                     // %")

        + ' AND ((Price) <= '

        + PriceComboBox.text

        + ')

        );

     

    end;

Open in new window

0
 

Author Comment

by:ST3VO
ID: 20326815
I'm getting a syntax error:

if sModels.Text<>'ANY' then
    Begin
      ADOQuery1.SQL.Add(
        'Select * from motors where (Make) = '
        + Quotedstr(ComboBox1.text)
        + ' AND ((Model) LIKE "%'  
        + ComboBox2.text            //Fiesta
        + '%")'                     // %")
        + ' AND ((Price) <= '
        + PriceComboBox.text
        + ') <-- HERE
        );
     
    end;

Undetermined String...
0
 
LVL 19

Expert Comment

by:Limbeck
ID: 20326838
add a ' after the )

but also test if the price is a valid price

f.i.

var iDummy:integer;
if sModels.Text<>'ANY'  then
try
  iDummy=StrToInt(PriceComboBox.text);
      ADOQuery1.SQL.Add(
        'Select * from motors where (Make) = '
        + Quotedstr(ComboBox1.text)
        + ' AND ((Model) LIKE "%'  
        + ComboBox2.text            //Fiesta
        + '%")'                     // %")
        + ' AND ((Price) <= '
        + PriceComboBox.text
        + ') '
        );
  //etc
except
  //errorhandling
end;
0
 

Author Comment

by:ST3VO
ID: 20326858
Hmmm...Query won't run...Sytax Error :o/
0
 
LVL 19

Expert Comment

by:Limbeck
ID: 20326868
what are the values for PriceComboBox.TEXT,ComboBox2.text  & ComboBox1.text ?
0
 

Author Comment

by:ST3VO
ID: 20326891
Here is the Exact error message:

Syntax error in query expression '(Make) = 'Ford' AND ((Model) LIKE "%Mondeo%")
Select * From motors where (Make)='Ford' AND ((Model) LIKE "%Mondeo%") AND ((Price) <=10000)'

Hope it helps!!!

0
 
LVL 13

Accepted Solution

by:
rfwoolf earned 250 total points
ID: 20326982
Hmm... do you know if your Price field is an integer or string or what... but it might not even matter ...

Also I suppose it wouldn't hurt to add a bracket around the first condition...



var iDummy:integer;

if sModels.Text<>'ANY'  then

try

  iDummy=StrToInt(PriceComboBox.text);

      ADOQuery1.SQL.Add(

        'Select * from motors where ((Make) = '

        + Quotedstr(ComboBox1.text) 

        + ') AND ((Model) LIKE "%'  

        + ComboBox2.text         

        + '%") AND ((Price) <= '

        + PriceComboBox.text

        + ')'

        );

  //etc

except

  //errorhandling

end;
 
 

////ALSO TRY:
 
 

var iDummy:integer;

if sModels.Text<>'ANY'  then

try

  iDummy=StrToInt(PriceComboBox.text);

      ADOQuery1.SQL.Add(

        'Select * from motors where ((Make) = '

        + Quotedstr(ComboBox1.text) 

        + ') AND ((Model) LIKE "%'  

        + ComboBox2.text         

        + '%") AND ((Price) <= "'

        + PriceComboBox.text

        + '")'

        );

  //etc

except

  //errorhandling

end;

Open in new window

0
 

Author Comment

by:ST3VO
ID: 20327010
The Query now works but the Price is not working properly!

I selected Price of 4000 and it's showing prices of 10000 etc  

Any ideas?

Thanks

ST3VO
0
 
LVL 19

Expert Comment

by:Limbeck
ID: 20327031
what type is the field price in the database? sounds like it is a string/varchar; not a int :)
0
 

Author Comment

by:ST3VO
ID: 20327050
Yes it's a string as it's reading from a CSV file
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20327058
why don't you use format() ? In generally it makes your code much better readable with such complex string 'generation'
if sModels.Text<>'ANY'  then

  ADOQuery1.SQL.Add(Format('select * from motors where Make = %s AND ((Model like "%%%s%%") and ((Price <= %s)',

                           [QuotedStr(ComboBox1.Text), ComboBox2.Text, PriceCombobox.Text]);

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 19

Expert Comment

by:Limbeck
ID: 20327077
not sure if this will work but try:

       + '%") AND (Val(Price) <= '

instead of        
      + '%") AND ((Price) <= '

0
 

Author Comment

by:ST3VO
ID: 20327125
MerijnB:

I get a Syntax error with your code see below:
Missing ),], or item in query expression 'Make = 'Ford' AND ((Model like "%Mondeo%") AND ((Price <= 4000)'

Limbeck:
Syntax  error too.

Sorry guys :o/

0
 
LVL 19

Assisted Solution

by:MerijnB
MerijnB earned 250 total points
ID: 20327132
oops...

can you check the typedef of the price table...?
if sModels.Text<>'ANY'  then

  ADOQuery1.SQL.Add(Format('select * from motors where Make = %s AND ((Model like "%%%s%%") and (Price <= %s))',

                           [QuotedStr(ComboBox1.Text), ComboBox2.Text, PriceCombobox.Text]);

Open in new window

0
 

Author Comment

by:ST3VO
ID: 20327148
Hmmm...

Type Mismatch error

0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20327155
where do you get this error?
0
 

Author Comment

by:ST3VO
ID: 20327236
After running the Query
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20327264
so, this error comes from the DB, not from delphi?

Can you post the resulting query?
0
 
LVL 19

Expert Comment

by:Limbeck
ID: 20327403
might be the "" . try it like this:

var sLike:String;
sLike:='%'+ComboBox2.text+ '%'

     + ') AND ((Model) LIKE '  
        + Qutedstr(sLike)
0
 
LVL 19

Expert Comment

by:Limbeck
ID: 20327519
Quotedstr(sLike)
0
 

Author Comment

by:ST3VO
ID: 20327547
Here is the whole code:

Hope it helps!

Thanks again

ST3VO

procedure TForm1.Button12Click(Sender: TObject);

 var WhereClause: string;

    WhereClauseParts: TStringList;

    i: integer;

    Query: string;

begin

   
 

  ADOQuery1.active := false;

  ADOQuery1.Close;

  ADOQuery1.SQL.Clear;
 

  /////////////////////////////////////////////////////////////
 
 

begin

 WhereClauseParts := TStringList.Create();
 

 // check make combobox

 if sco.Text <> 'ANY' then

  WhereClauseParts.Add(Format('(Make = %s)', [QuotedStr(sco.Text)]));
 

 // check model combobox

 if sModels.Text <> 'ANY' then

  WhereClauseParts.Add(Format('(Model like "%%%s%%")', [sModels.Text]));
 

 // check price combobox

 if sPrice.Text <> 'ANY' then

  WhereClauseParts.Add(Format('(Price <= %s)', [sPrice.Text]));
 

 // check postcode combobox

 if sPostCode.Text <> 'ANY' then

  WhereClauseParts.Add(Format('(Postcode like "%s%%")', [Copy(sPostCode.Text, 1, 3)]));
 

 // now combine all where clauses

 if WhereClauseParts.Count > 0 then

 begin

  WhereClause := ' where';

  for i := 0 to WhereClauseParts.Count - 1 do

  begin

   WhereClause := WhereClause + ' ' + WhereClauseParts[i];
 

   if i < WhereClauseParts.Count - 1 then

    WhereClause := WhereClause + ' AND';

  end;

 end;
 

 WhereClauseParts.Free();
 
 
 

 // now we have the where clause, make the rest of the query

 Query:=('select * from motors' + WhereClause);

end;
 
 
 
 
 

 ////////////////////////////////////////////////////////////////
 

   if not sCheckBox1.Checked then

   begin

  ADOQuery1.SQL.Add('UNION  ALL SELECT TOP 200 * from motors where Make <> "' + (sco.Text) + '"');

   end;
 

//  ADOQuery1.Active := true;

 //  ADOQuery1.Open;
 

   if ADOQuery1.EOF  then

  begin

    sMessageDlg('Sorry No Matches Found! Please try another search!', mtInformation, [mbOK], 0);
 

  Exit;

end;
 
 

 

 

end;

Open in new window

0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20327570
I don't see where you use the contents of the string 'Query'?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Get height of a forms top border. 3 32
Delphi 10 Seattle Dataset Actions 5 71
Help on project with Soap 10 44
Delphi selector screen 2 58
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now