http://www.delphi2007.net/DelphiDB/html/delphi_20061215102418294.html
var
strsql:string;
temp:string;
temp:=edt1.text;
strsql:='select * from 人事档案 where 部门='+''南京分公司\\''+temp';
qry1.close;
qry1.sql.clear;
qry1.sql.add(strsql);
qry1.open;
我的意思是执行一条sql 语句:
select * from 人事档案 where 部门='南京分公司\\temp'
老是报错,估计是逗号问题,怎么办?
strsql:='select * from 人事档案 where 部门='+''南京分公司\\''+temp';
不报错就希奇了
你这 南京分公司\\ 和 temp 都是什么??
给你写好!
var
strsql:string;
temp:string;
begin
temp:=edt1.text;
strsql:='select * from 人事档案 where 部门=%s\\%s';
qry1.close;
qry1.sql.text:=format(strsql,['南京分公司',temp]);
qry1.open;
end;
搞定!
这样写即使正确也容易把人搞糊涂:
strsql := 'select * from 人事档案 where 部门='+''南京分公司\\''+temp';
我喜欢下面的写法:
strsql := 'select * from 人事档案 where 部门=' + #39 + '南京分公司\\' + temp + #39;
strsql:='select * from 人事档案 where 部门='+'''南京分公司\\temp''';
本文介绍在使用Delphi进行数据库操作时,如何正确地拼接SQL查询语句,避免语法错误和潜在的安全风险。通过实例演示了使用字符串格式化和转义字符的方法,确保SQL语句能够正确执行。

931

被折叠的 条评论
为什么被折叠?



