Delphi怎么判断数据库中某列是否包含某值

zzyhost 2015-03-13 07:24:16
若不包含就把数据写进去,若包含了就不写了,Delphi代码怎么判断?
...全文
332 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
doloopcn 2015-03-14
  • 打赏
  • 举报
回复
引用 2 楼 u013402605 的回复:
[quote=引用 1 楼 doloopcn 的回复:] query.close; query.sql.clear; query.sql.add('select 字段名 from 表 where 字段名 like '+quotedstr('%包含的字符%')); try query.open; except end; if query.active then if query.recordcount<1 then begin //包含,写入你的代码 ; end;
谢谢,再问一下,if query.active 为什么要判断这个?[/quote] 防止上面的exception发生后,对query操作会直接raise错误,导致程序不能继续执行
lyhoo163 2015-03-14
  • 打赏
  • 举报
回复
编写一个函数:
function HasStr(sTableName, sField, S: string): boolean;
var SQL:string;
begin
  Result := False;
  SQL:='select '+sField+' from '+sTableName+' where '+sField+'='+#39+S+#39;
  query.close;
  query.sql.clear;
  query.sql.add(S);
  query.open;
  if query.recordcount>0 then
  Result := True;
end;
条件query已与连接数据库控件相关。
zzyhost 2015-03-13
  • 打赏
  • 举报
回复
引用 1 楼 doloopcn 的回复:
query.close; query.sql.clear; query.sql.add('select 字段名 from 表 where 字段名 like '+quotedstr('%包含的字符%')); try query.open; except end; if query.active then if query.recordcount<1 then begin //包含,写入你的代码 ; end;
谢谢,再问一下,if query.active 为什么要判断这个?
doloopcn 2015-03-13
  • 打赏
  • 举报
回复
query.close; query.sql.clear; query.sql.add('select 字段名 from 表 where 字段名 like '+quotedstr('%包含的字符%')); try query.open; except end; if query.active then if query.recordcount<1 then begin //包含,写入你的代码 ; end;

2,498

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧