我不知道如何从 Delphi 中具有许多条件的表中进行选择,这是我的代码
qr_catdegree.Close;
qr_catdegree.SQL.Clear;
qr_catdegree.SQL.Add('select * from cat_degreee where id_categ =+inttostr(id_categ) and id_degree=+`inttostr(id_degree)' );
//qr_catdegree.SQL.Add(');
qr_catdegree.Open;
Indice_degree := qr_catdegree.FieldByName('Indice_degree').Asinteger;
edit15.text := inttostr(Indice_degree) ;`
我认为您会感到困惑,因为您正试图在对 SQL.Add 的调用中构建您的 Sql。这掩盖了您尝试中的语法错误。
试试这个
var
S : String;
[...]
qr_catdegree.Close;
qr_catdegree.SQL.Clear;
S := 'select * from cat_degreee where id_categ = ' + inttostr(id_categ);
// do you REALLY mean 'degreee' and not 'degree'???
S := S + ' and id_degree = ` + inttostr(id_degree);
qr_catdegree.SQL.Add(S);
顺便说一句,您会注意到 Ken White 的评论建议使用参数而不是您目前正在执行的操作。我同意他的建议有两个原因:
首先,在预先准备好的 SQL 查询中使用参数会减少服务器的开销,因为它只需要解析和编译一次查询,而不是每次执行查询时。使用参数,您可以使用不同的参数值重新执行查询多次。
其次,它只是从包括像表达式一小步,+ IntToStr(SomeInteger)
在SQL中,包括像+ Edit1.Text
,你真的应该避免以往任何时候都这样做。原因是它使您的代码面临“SQL 注入”的风险(请参阅https://en.wikipedia.org/wiki/SQL_injection),因为它使恶意用户有机会在他们的内容中添加额外的 SQL 命令输入到 TEdit。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句