[英]Can't get delphi SQL LIKE to work with %
I'm doing a school project and need to code a query to filter a dataset to certain variables. 我正在做一个学校项目,需要对查询进行编码以将数据集过滤为某些变量。 All my SQL works fine, except I can't get the LIKE statement to work with %-signs.
我所有的SQL都工作正常,除了我无法使LIKE语句与%符号一起使用。 I believe my syntax is wrong.
我相信我的语法是错误的。 Can anybody please tell me what I'm doing wrong.
谁能告诉我我在做什么错。 Thanks
谢谢
The code: 编码:
qryMovie.SQL.Clear;
qryMovie.SQL.Add('SELECT * FROM Movies');
qryMovie.SQL.Add('WHERE Genre = ' + QuotedStr(genre));
qryMovie.SQL.Add('AND Price BETWEEN ' + minPrice + ' AND ' + maxPrice);
qryMovie.SQL.Add('AND Title LIKE %' + title + '%');
qryMovie.Open;
Error produced: 产生错误:
'Syntax error in query expression 'Genre = 'Action/Adventure' AND Price BETWEEN 0 AND 200 AND Title LIKE %Star Wars%'' '查询表达式'Genre ='Action / Adventure'中的语法错误,且价格介于0和200之间,且标题类似%Star Wars%
LIKE %Star Wars%
喜欢%Star Wars%
but you need 但是你需要
LIKE '%Star Wars%'
喜欢'%Star Wars%'
You need to quote %
with '
: 您需要用
'
引用%
:
qryMovie.SQL.Add(' AND Title LIKE ''%' + title + '%''');
Anyway you should use binded parameters instead of concatenating SQL string. 无论如何,您应该使用绑定参数而不是连接SQL字符串。 It is error-prone and could lead to SQL Injection attacks.
它易于出错,并可能导致SQL注入攻击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.