0

I am trying to execute a SQL query using Zeolibs but compiler keeps complaining with the following error - Unknown column in Where clause.

ZQuery1.SQL.Text := 'SELECT * FROM new_table WHERE TagName = '+theSig.f.TagName;
ZQuery1.ExecSQL;

But I checked all the columns in my database table and it is correct as I have it here TagName

What is wrong with my SQL statement?

dario
  • 5,149
  • 12
  • 28
  • 32
ThN
  • 3,235
  • 3
  • 57
  • 115

1 Answers1

3

You need to use the QuotedStr function. So your code will be

ZQuery1.SQL.Text := 'SELECT * FROM new_table WHERE TagName = '+QuotedStr(theSig.f.TagName);

A better alternative is to use parametrized queries (this will block attempts for SQL injection - take a quick look at this question).

ZQuery1.SQL.Text := 'SELECT * FROM new_table WHERE TagName = :myparam';
ZQuery1.ParamByName('myparam').AsString := theSig.f.TagName;
Community
  • 1
  • 1
RBA
  • 12,337
  • 16
  • 79
  • 126
  • You are right I apparently needed double quotes around the TagName value. I played around with statement and got it to work. `SELECT * FROM new_table WHERE TagName = " '+theSig.f.TagName+' " ';` – ThN Jan 16 '15 at 15:39
  • 1
    It is better to use parametrized queries instead of lots and lots of quotas. – RBA Jan 16 '15 at 15:43