DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: tkzcol
今日帖子: 21
在线用户: 12
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 happyfox (happyfox) ★☆☆☆☆ -
普通会员
2003/12/17 9:45:01
标题:
大家来帮忙看看这段代码的错误,急!在线等! 浏览:2348
加入我的收藏
楼主: var
  i:integer;
  Str,s:String;
  SQLTime:boolean;

begin
  SQLTime:=True;
  s:='select * from jshjk.db where a<>nil';
  for i := 0 to jshjcx.ComponentCount-1 do
  begin
   try
    str:=jshjcx.Components[i].Name;
    if ((jshjcx.Components[i] is Tcombobox) //判断是不是COMBOBOX且不能为时间相关的组合框
    and((jshjcx.Components[i] as Tcombobox).text<>'))then
      if not((str=ComboboxYear.Name) or (str=ComboboxMonth.Name)
        or(str=ComboboxSecYear.Name)or (str=ComboboxSecMonth.Name) )then
        s:=s+' and '+copy(jshjcx.Components[i].Name,(Length(jshjcx.Components[i].Name)-3),4)+'='  //限制字段长度为了4
        +(jshjcx.Components[i] as Tcombobox).text
      else //如果时间相关的年份有数据的话
        if ((ComboboxYear.text<>')  and SQLTime)then
        begin
          s:=s+' and hjsj between :date1 and :date2';
          SQLTime:=false;
        end;
    if ((jshjcx.Components[i] is TEdit) //如果是文本框控件的话
      and((jshjcx.Components[i] as TEdit).text<>'))then
      s:=s+' and '+copy(jshjcx.Components[i].Name,(Length(jshjcx.Components[i].Name)-3),4)+'='  //限制了字段长度必须为4
      +(jshjcx.Components[i] as TEdit).text;
   except
     exit;
   end;

  end;
  showmessage(s); //用于测试,到这一步没问题,应是S相加之后在执行SQL时出的问题
end;
//设置完SQL语句之后开始查询
with query1 do
begin
  Close;
  SQL.Clear;
  SQL.Add(s);
  ParamByName('date1').AsDate:=
    EncodeDate(StrToInt(ComboBoxYear.Text),StrToInt(ComboBoxMonth.Text),1);
  ParamByName('date2').AsDate:=
    EncodeDate(StrToInt(ComboBoxSecYear.Text),StrToInt(ComboBoxSecMonth.Text),30);
  prepare;
  open;
end;
此帖子包含附件:
JPEG 图像
大小:53.7K
----------------------------------------------
无论你是狮子还是羚羊,为了生存,你别无选择,你必须跑!
作者:
男 happyfox (happyfox) ★☆☆☆☆ -
普通会员
2003/12/17 9:45:38
1楼: 出错如下
此帖子包含附件:
JPEG 图像
大小:20.7K
----------------------------------------------
无论你是狮子还是羚羊,为了生存,你别无选择,你必须跑!
作者:
男 happyfox (happyfox) ★☆☆☆☆ -
普通会员
2003/12/17 9:50:04
2楼: copy(jshjcx.Components[i].Name,(Length(jshjcx.Components[i].Name)-3),4)组件名后四位为相应的字段名
----------------------------------------------
无论你是狮子还是羚羊,为了生存,你别无选择,你必须跑!
作者:
男 xuwenyao11 (贝贝11) ★☆☆☆☆ -
盒子活跃会员
2003/12/17 10:10:44
3楼: 你看看你的那个字符串为空了,


----------------------------------------------
-
作者:
男 heihei_76 (小黑) ★☆☆☆☆ -
盒子活跃会员
2003/12/17 10:28:50
4楼: 是啊﹐你說說執行到哪行出這個錯誤,
----------------------------------------------
-

﹗﹗﹗
作者:
男 happyfox (happyfox) ★☆☆☆☆ -
普通会员
2003/12/17 10:36:14
5楼: 在执行SQL查询时出的错即便是选一个组别也出错应是SQL格式或a<>nil那出的错
----------------------------------------------
无论你是狮子还是羚羊,为了生存,你别无选择,你必须跑!
作者:
男 happyfox (happyfox) ★☆☆☆☆ -
普通会员
2003/12/17 10:41:53
6楼: 这两句话哪个对呢
select * from jshjk.db where a<>nil and jszb=语文组
select * from jshjk.db where a<>nil and jszb='语文组'
很可能是这个地方出的错知道的朋友请速回答
----------------------------------------------
无论你是狮子还是羚羊,为了生存,你别无选择,你必须跑!
作者:
男 jiangmao (风云) ★☆☆☆☆ -
普通会员
2003/12/17 11:58:02
7楼: 下一个
----------------------------------------------
-
作者:
男 happyfox (happyfox) ★☆☆☆☆ -
普通会员
2003/12/17 13:09:44
8楼: 请看这个小刘老师
此帖子包含附件:
JPEG 图像
大小:21.2K
----------------------------------------------
无论你是狮子还是羚羊,为了生存,你别无选择,你必须跑!
作者:
男 happyfox (happyfox) ★☆☆☆☆ -
普通会员
2003/12/17 13:27:19
9楼: procedure Tjshjcx.ButtonSearchClick(Sender: TObject);
 var
  i:integer;
  Str,s:String;
  SQLTime:boolean;
begin
//判断各选项是否为空
{if ((ComboBoxJszb.Text=')and(ComboBoxJsxm.Text=')and(EditJxmc.Text=')
  and(ComboBoxZxmc.Text=')and(ComboBoxjxlb.Text=')and(ComboBoxJxdj.Text=')
  and(ComboBoxYear.Text=')and(ComboBoxSecYear.Text='))then
begin
  Application.MessageBox('当前查询输入为空,请输入查询项目!','提示',mb_ok);
  exit;
end; }
//判断起止年份是否填写
if (((comboboxYear.Text=')and(comboboxSecYear.Text<>'))
  or((comboboxYear.Text<>')and(comboboxSecYear.Text='))) then
begin
  Application.MessageBox('年份设置不正确,请重新设置!','提示',mb_ok);
  exit;
end;
//判断起止年份是否正确2不能小于1
if ((comboboxYear.Text<>')and (comboboxSecYear.Text<>')) then
  if(StrToInt(comboboxYear.Text)>StrToInt(comboboxSecYear.Text)) then
  begin
    Application.MessageBox('起始年份数不能大于结束年份!','提示',mb_ok);
    exit;
  end;
//判断各框是否为空,不为空的话作为查询参数输入到SQL语句中去
begin
  SQLTime:=True;
  s:='select * from jshjk.db where :a';
  for i := 0 to jshjcx.ComponentCount-1 do
  begin
   try
    str:=jshjcx.Components[i].Name;
    if ((jshjcx.Components[i] is Tcombobox) //判断是不是COMBOBOX且不能为时间相关的组合框
    and((jshjcx.Components[i] as Tcombobox).text<>'))then
      if not((str=ComboboxYear.Name) or (str=ComboboxMonth.Name)
        or(str=ComboboxSecYear.Name)or (str=ComboboxSecMonth.Name) )then
        s:=s+' and '+(copy(jshjcx.Components[i].Name,(Length(jshjcx.Components[i].Name)-3),4)+'='+''+(jshjcx.Components[i] as Tcombobox).text+'')
      else //如果时间相关的年份有数据的话
        if ((ComboboxYear.text<>')  and SQLTime)then
        begin
          s:=s+' and hjsj between :date1 and :date2';
          SQLTime:=false;
        end;
    if ((jshjcx.Components[i] is TEdit) //如果是文本框控件的话
      and((jshjcx.Components[i] as TEdit).text<>'))then
      s:=s+' and '+copy(jshjcx.Components[i].Name,(Length(jshjcx.Components[i].Name)-3),4)+'='  //限制了字段长度必须为4
      +''+(jshjcx.Components[i] as TEdit).text+'';
   except
     exit;
   end;

  end;
  showmessage(s); //用于测试,到这一步没问题,应是S相加之后在执行SQL时出的问题
end;
//设置完SQL语句之后开始查询
with query1 do
begin
  Close;
  SQL.Clear;
  SQL.Add(s);
  ParamByName('a').AsBoolean:=True;
  //ParamByName('date1').AsDate:=
   // EncodeDate(StrToInt(ComboBoxYear.Text),StrToInt(ComboBoxMonth.Text),1);
 // ParamByName('date2').AsDate:=
   // EncodeDate(StrToInt(ComboBoxSecYear.Text),StrToInt(ComboBoxSecMonth.Text),30);
  prepare;
  open;
end;

{s:='select * from jshjk.db where ';
begin
  for i:=0 to Self.ControlCount-1 do
    begin
      if ((controls[i] is TcomBobox) or (controls[i] is TEdit)) then
      begin
      str:=Controls[i].Name;
      if ((str<>ComboboxYear.Name) or (str<>ComboboxMonth.Name)
       or(str<>ComboboxSecYear.Name)or (str<>ComboboxSecMonth.Name) )then
      begin
        s:=s+controls[i]
      end;
      end;
    end;
end;}

end;
----------------------------------------------
无论你是狮子还是羚羊,为了生存,你别无选择,你必须跑!
作者:
男 plates (小刘老师) ★☆☆☆☆ -
普通会员
2003/12/17 13:48:54
10楼: var
 OrAnd,S,SqlTemp:string;
 j:integer;
begin
  //根据需要设置连接查询条件OrAnd为 or 或and 可用一个checkbox1 判断
  if checkbox1.checked then OrAnd:='OR'
     else OrAnd:='OR';

  SQLTime:=True;
  SqlTemp:='select * from jshjk.db where ';
  s:=';
  j:=0;
  for i := 0 to jshjcx.ComponentCount-1 do
  begin
   try
    str:=jshjcx.Components[i].Name;
    if ((jshjcx.Components[i] is Tcombobox) //判断是不是COMBOBOX且不能为时间相关的组合框
    and((jshjcx.Components[i] as Tcombobox).text<>'))then
      if not((str=ComboboxYear.Name) or (str=ComboboxMonth.Name)
        or(str=ComboboxSecYear.Name)or (str=ComboboxSecMonth.Name) )then
        begin//注意这里这是我加的      
          if j>0 then s:=OrAnd+s ;//其它地方就不用再考虑查询条件了,你静态地加上了 and 如果需要加 or 你又怎么办了 
                                  //其它地方和这里差不多
           s:=s+(copy(jshjcx.Components[i].Name,(Length(jshjcx.Components[i].Name)-3),4)+'='+''+(jshjcx.Components[i] as Tcombobox).text+'');
        end
      else //如果时间相关的年份有数据的话
        if ((ComboboxYear.text<>')  and SQLTime)then
        begin
          s:=s+' and hjsj between :date1 and :date2';
          SQLTime:=false;
        end;
    if ((jshjcx.Components[i] is TEdit) //如果是文本框控件的话
      and((jshjcx.Components[i] as TEdit).text<>'))then
      s:=s+' and '+copy(jshjcx.Components[i].Name,(Length(jshjcx.Components[i].Name)-3),4)+'='  //限制了字段长度必须为4
      +''+(jshjcx.Components[i] as TEdit).text+'';
   except
     exit;
   end;

  end;
  showmessage(s); //用于测试,到这一步没问题,应是S相加之后在执行SQL时出的问题
end;
//设置完SQL语句之后开始查询
with query1 do
begin
  Close;
  SQL.Clear;
  SQL.Add(sqlTemp+s);//这里做了修改
  ParamByName('a').AsBoolean:=True;
  //ParamByName('date1').AsDate:=
   // EncodeDate(StrToInt(ComboBoxYear.Text),StrToInt(ComboBoxMonth.Text),1);
 // ParamByName('date2').AsDate:=
   // EncodeDate(StrToInt(ComboBoxSecYear.Text),StrToInt(ComboBoxSecMonth.Text),30);
  prepare;
  open;
end;


----------------------------------------------
-
作者:
男 heihei_76 (小黑) ★☆☆☆☆ -
盒子活跃会员
2003/12/17 14:11:09
11楼: 要注意,
sql:='select * from table1 where a<>nil';這里的a<>nil我只是說隨便加一個不會影響結果的.當類型要相同,如果字段a是字符型的則a<>'
----------------------------------------------
-

﹗﹗﹗
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行140.625毫秒 RSS