0

I've created the query below as a filter command. But it only works on integer fields. It seems whenever the field is a string it does not work...

The error i keep getting is 'SQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like'%a%''at line 1'

My Code is as follows:

datalive.mail_queue.SQL.Text:='select*from mail_queue where user_id=:user_id and '+fieldname+' like :value';
Addparam(datalive.mail_queue,'user_id',ftString,inttostr(user_id));
Addparam(datalive.mail_queue,'fieldname',ftString,fieldname);
Addparam(datalive.mail_queue,'value',ftString,'%'+edit1.Text+'%');
datalive.mail_queue.Active:=true;
2
  • 1
    have you tried the query in MySQL? I believe this is not correct - "select*from ...". it should be 'select * from '. Commented Jun 17, 2015 at 7:37
  • Hi @RBA, it runs successfully with everything else. So it should not be a problem. I have been typing it like that for years with no problem. Commented Jun 17, 2015 at 9:15

1 Answer 1

1

The problem seems to be in your query string, before the like you are passing a parameter concatenating the string, so I suggest to you to change your query string in this way :

datalive.mail_queue.SQL.Text:=' select * from mail_queue where user_id=:user_id and '+fieldname+' like :value';

You don't have to pass the parameter fieldname since it is a column and not a value :

Addparam(datalive.mail_queue,'user_id',ftString,inttostr(user_id));
Addparam(datalive.mail_queue,'value',ftString,'%'+edit1.Text+'%');

Be sure that the fieldname variable doesn't contain a quoted string, but simply the name of the column to filter on ...

Sign up to request clarification or add additional context in comments.

2 Comments

I changed it like you suggested. But I am still getting the same error.
Also, when changing the '+fieldname+' to :fieldname. It does not give me the error. But something goes wrong with the filter and there are just no records in the db whatsoever... I do not know if that may be a clue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.