Search code examples
sqldatabasedelphiwhere-clausezeos

Zeolibs: Unknown column in Where clause


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?


Solution

  • 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;