var
  i:array[1..15] of String;
  j:integer;
  str:string;
begin
str:='select ';
  for j:=0 to listbox1.Items.Count-1 do
    begin
       if listbox1.Items[j]='厂商识别代码' then
         begin
           i[j]:='firm_id';
         end;
       if listbox1.Items[j]='全球位置码' then
         begin
            i[j]:='Gln';
         end;
       if ListBox1.Items[j]='全球贸易项目代码' then
         begin
            i[j]:='GTIN';
         end;
    if j<ListBox1.Items.Count-1  then
    str:=Str+' i['''+inttostr(j)+'''],'
  else
      Str:=str+' i['''+inttostr(j)+''']';
    end;
  Str:=Str+' from Baseinfo';
   With Query1 do
     begin
        close;
        sql.Clear;
        sql.Add(str);
        open;
     end;
   end;

解决方案 »

  1.   

    这个SQL语句有错误,请问该怎么来写恩?
      

  2.   

    好丑陋的代码呀。你老师是谁呀???我吐。
    一看就知道是又是一个不懂OOP的家伙。
    只会拿Delphi当VB用。难很多人就说写Delphi的人太烂。var
      i:array[1..15] of String; //要这么多个下标干嘛?或如果还不够多呢?
                                //还有下面J=0的时候呢?
      j:integer;
      str:string;
    begin
    str:='select ';
      for j:=0 to listbox1.Items.Count-1 do
        begin
           if listbox1.Items[j]='厂商识别代码' then  //<---像这样写程序也真够累的
             begin
               i[j]:='firm_id';
             end;
           if listbox1.Items[j]='全球位置码' then
             begin
                i[j]:='Gln';
             end;
           if ListBox1.Items[j]='全球贸易项目代码' then
             begin
                i[j]:='GTIN';
             end;
        if j<ListBox1.Items.Count-1  then
        str:=Str+' i['''+inttostr(j)+'''],'  //<----这里是问题所在
      else
          Str:=str+' i['''+inttostr(j)+''']';  //<--- Here, too....
        end;
      Str:=Str+' from Baseinfo';
       With Query1 do
         begin
            close;
            sql.Clear;
            sql.Add(str);
            open;
         end;
       end;
    ===================================================================var
      s : TStringList;
      j : Integer;
    begin
      s := TStringList.Create;
      for j:=0 to listbox1.Items.Count-1 do
      begin
        if listbox1.Items[j]='厂商识别代码' then  //<---没办法,前提已定,只能这样写了.
                                                  //我一般是用Items.Objects挂对象的
          s.add('firm_id')
        else if listbox1.Items[j]='全球位置码' then
          s.add('Gln')
        else if ListBox1.Items[j]='全球贸易项目代码' then
          s.add('GTIN')
        else //<---不可能只有这三个字段吧。
          ......
      end;
      try
        if s.count > 0 then
          with Query1 do begin
            if active then close;
            Sql.Text := 'Select ' + s.CommaText + ' from BaseInfo';
            Open;
          end
        else
          raise Exception.Create('列表为空!');
      finally
        s.free;
      end;
      

  3.   

    楼上兄台骂的是,很对!
    我没有师傅,从头到尾都是我一个人自学而来,请多多照顾,
    底下我一定都找书籍来学习OOP,
    之前我也看过OOP,可是有些地方就是不很明白,请多多指教
    在下,谢谢了!