delid[1]和delid[2]在前面的代码已经赋值。下面是我写的代码,但是老是提示insert语法错误,请高手帮忙看看,谢谢!
说明一下,运行的结果是建表和修改表都是没错的,就是insert的时候出错。
procedure TForm1.Button17Click(Sender: TObject);
type TArr = array[1..8] of real;
     del = array[1..2] of integer;
var delid:del;
    i:integer;
    infoD:real;
    k:integer;
    m:string;
    n:string;
    infovd:real;
    max_gain:string;
    temp:real;
    gainvd:Tarr;
    gainmax:real;
    querystring:string;
    querystring1:string;
    querystring2:string;
const infoID:array[1..8] of string =('id1','id2','id3','id4','id5','id6','id7','id8');
const Gain:array[1..8] of string =('id1','id2','id3','id4','id5','id6','id7','id8');
const tableold:array[1..2] of string =('T11','T10');
const tablenew:array[1..2] of string =('T21','T20');Function Log(X,Y:double):double;
begin
  Try
    Result:=Ln(X)/Ln(Y);
  except
    Result:=0;
  end;
end;function sum1(id:string;h:integer):integer;
begin
with adoquery1 do
begin
close;
sql.Clear;
sql.Add('select sum(id1) from '+tableold[h]+' where '+id+'=1');
open;
result:=adoquery1.Fields[0].Asinteger;
end;
end;function sum2(id:string;h:integer):integer;
begin
with adoquery1 do
begin
close;
sql.Clear;
sql.Add('select sum(id1) from '+tableold[h]+' where '+id+'=0');
open;
result:=adoquery1.Fields[0].Asinteger;
end;
end;function count0(id:string;h:integer):integer;
begin
with adoquery1 do
begin
close;
sql.Clear;
sql.Add('select count('+id+') from '+tableold[h]+' where '+id+'=1');
open;
result:=adoquery1.Fields[0].Asinteger;
end;
end;function count1(id:string;h:integer):integer;
begin
with adoquery1 do
begin
close;
sql.Clear;
sql.Add('select count('+id+') from '+tableold[h]+'');
open;
result:=adoquery1.Fields[0].Asinteger;
end;
end;
function count2(id:string;h:integer):integer;
begin
with adoquery1 do
begin
close;
sql.Clear;
sql.Add('select count('+id+') from '+tableold[h]+' where '+id+'=0');
open;
result:=adoquery1.Fields[0].Asinteger;
end;
end;function info(i: integer): real;
begin
result:=-(sum1(infoID[i],k)/count0(infoID[i],k)*log(sum1(infoID[i],k)/count0(infoID[i],k),2)-(1-sum1(infoID[i],k)/count0(infoID[i],k))*log((1-sum1(infoID[i],k)/count0(infoID[i],k)),2));
end;function info2(i: integer): real;
begin
result:=-(sum2(infoID[i],k)/count2(infoID[i],k)*log(sum2(infoID[i],k)/count2(infoID[i],k),2)-(1-sum2(infoID[i],k)/count2(infoID[i],k))*log((1-sum2(infoID[i],k)/count2(infoID[i],k)),2));
end;begin
for k:=1 to 2 do
  begin
  infod:=-(sum1('id1',k)/count1('id1',k)*log(sum1('id1',k)/count1('id1',k),2)-(1-sum1('id1',k)/count1('id1',k))*log(1-sum1('id1',k)/count1('id1',k),2));
  gainmax:=0;
  max_gain:=gain[1];  for i:=2 to 8 do
    begin
    if i<>delid[k] then
      begin
      infovd:=info(i)*count0(infoID[i],k)/count1(infoID[i],k)+info2(i)*count2(infoID[i],k)/count1(infoID[i],k);
      gainvd[i]:=infod-infovd;
      if gainvd[i]>gainmax then
        begin
        gainmax:=gainvd[i];
        max_gain:=gain[i];
        b8:=i;
        end;
      with adoquery1 do
        begin
        close;
        sql.Clear;
        sql.add('insert into T (id,gain,infovd,infod) values('+inttostr(i)+','+floattostr(gainvd[i])+','+floattostr(infovd)+','''+max_gain+''')');
        execsql;
        end;
      end;
    end;
  with adoquery1 do
    begin
    close;
    sql.clear;
    sql.add('create table '+tablenew[k]+inttostr(1)+' ');
    execsql;
    end;
  with adoquery1 do
    begin
    close;
    sql.clear;
    sql.add('create table '+tablenew[k]+inttostr(0)+' ');
    execsql;
    end;  for i:=1 to 8 do
    begin
    if i<>delid[k] then
      begin
      if max_gain<>gain[i] then
        begin
        with adoquery1 do
          begin
          close;
          sql.clear;
          sql.add('alter table '+tablenew[k]+inttostr(1)+' add '+gain[i]+' int');
          execsql;
          end;
        with adoquery1 do
          begin
          close;
          sql.clear;
          sql.add('alter table '+tablenew[k]+inttostr(0)+' add '+gain[i]+' int');
          execsql;
          end;
        if QueryString <>'' then
          begin
          QueryString:=QueryString+','+gain[i] ;
          end
          else
          QueryString:=gain[i];
          end;
        end;
      end;
    end;
  QueryString1:='insert into '+tablenew[k]+inttostr(1)+' SELECT '+QueryString+' FROM '+tableold[k]+' where '+max_gain+'=1';
  with adoquery1 do
    begin
    close;
    sql.clear;
    sql.add(QueryString1);
    execsql;
    end;
  QueryString2:='insert into '+tablenew[k]+inttostr(0)+' SELECT '+QueryString+' FROM '+tableold[k]+' where '+max_gain+'=0';
  with adoquery1 do
    begin
    close;
    sql.clear;
    sql.add(QueryString2);
    execsql;
    end;
 end;

解决方案 »

  1.   

    sql.add(QueryString1); 
    ShowMessage(QueryString1);//看SQL语句是否正确,或者用个Memo来保存SQL语句,如:Memo.Text:=QueryString1,然后拿到查询分析器执行一下试试
    execsql; 
      

  2.   

    将你的sql语句 打印在对话框中看看是不是对的.application.messagebox(pchar(QueryString2),notice,mb_ok);
      

  3.   

    1楼的大哥,我用Memo看了一下,显示insert into h,这是什么问题啊  
      

  4.   

    设置断点跟踪吧,这么老长,看的都晕菜了:(
    然后把出问题的SQL截出来,分析一下
      

  5.   

    应该是SQL的语句,语法的匹配的有问题
      

  6.   

    搞清楚了,是for循环里出了问题,代码写的比较乱,呵呵
    感谢回帖的各位大哥,特别是一楼的大哥