数据更新出了问题,我在保存学生信息的同时需要将学号、姓名保存到一张临时表中。但是在更新的时候提示由于将索引主关键字创建重复值的提示,保存不成功
请大虾给出修改时候同步更新临时表中数据的代码,多谢
学生表
stuid  学号
name   姓名
birth  生日
per_id  考勤卡号TMP表
per_name   姓名
per_card  生日
per_id  考勤卡号
考勤卡号是唯一的

解决方案 »

  1.   

    TMP表中还有一个stuid学号的字段和学生表中对应
      

  2.   

    现在主要是,每次修改tmp表中的内容更新不成功啊,请问如何处理
      

  3.   

    tmp表的主关键字不要不就行了
      

  4.   

    tmp表中是没有主关键字的,问题是我在beforpost的时候将学号,姓名等信息保存在tmp表中
    但是修改了某个信息如学号或姓名再保存的时候就出错了,修改的某个学生的信息不能同步更新到TMP表中,有谁能给出相关的代码,谢谢
      

  5.   

    未修改前先取出学号和姓名strOldID,strOldName
    修改后再取出新的学号和新的姓名strNewID,strNewName
    然后,if strOldID <> strNewID then
    begin
      strSQL := 'Update tmp set per_id= ' + strNewID +
       ' Where per_id='+ strOldID;
      //执行strSQL
    end;if strOldName <> strNewName then
    begin
      strSQL := 'Update tmp set per_name=' + QuotedStr(strNewName) +
        ' Where per_name = ' + QuotedStr(strOldName);
      //执行strSQL
    end;
      

  6.   

    fim的想法不错,但觉得有一个语义错误,
    第二个的if strOldName <> strNewName then
    好像是
    if strOldName = strNewName then
      

  7.   

    多谢fim,数据库是access的
    修改保存后,怎么知道修改后的记录就是原来的的记录呢?
    判断新旧字段的操作,应该在哪个事件中实现呢?
    请告之,谢谢
      

  8.   

    我是在学生表保存的时候做跟新的,学生表的用的是ADOquery,名称为AQ
    procedure TForm1.AQBeforePost(DataSet: TDataSet);
    begin
    if aq.State=dsinsert then
    begin
    aqtmp.Insert;
    aqtmp.FieldByName('stuid').AsString:=dbediteh2.Text;
    aqtmp.FieldByName('per_name').AsString:=dbediteh1.Text;
    aqtmp.FieldByName('per_card').AsString:=dbediteh3.Text;
    aqtmp.FieldByName('per_id').AsString:='01';
    aqtmp.FieldByName('per_yes').AsString:='N';
    aqtmp.Post;
    end;
    if aq.State=dsedit then
    begin
    aqtmp.FieldByName('stuid').AsString:=dbediteh2.Text;
    aqtmp.FieldByName('per_name').AsString:=dbediteh1.Text;
    aqtmp.FieldByName('per_card').AsString:=dbediteh3.Text;
    aqtmp.FieldByName('per_id').AsString:='01';
    aqtmp.FieldByName('per_yes').AsString:='N';
    aqtmp.Post;
    end;
    end;
      

  9.   

    用select 查找,如何有了就不保存,修改不行吗
      

  10.   

    果真和我想的一样,建议你养成定义主键的习惯,特别是在Access中,在你的临时表中加一个ID字段,自动编号,为主键。否则在更新的时候程序会提示无法更新定位行等等的错误。看不到你的代码,真的无从说起。我只能按照我的思路随便写一下。
    btnEdit.OnClick中strOldID   := FieldByName('per_id').AsString;
    strOldName := FieldByName('per_name').AsString;//开始修改
    Edit;
    FieldByName('per_id').AsString   := edtId.Text;
    FieldByName(per_name'').AsString := edtName.Text;
    Post;strNewID   := FieldByName('per_id').AsString;
    strNewName := FieldByName('per_name').AsString;然后是我刚才写的代码....
      

  11.   

    我试了一下,还是不行啊,发现修改某个学生学号后,将tmp中的另一条学生的学号更新了
    我将主要的代码贴出来,各位看看,看看错误的地方
    其中AQ是student表的ADOQUERY,AQTMP是TMP表的ADOQUERY,我将更新的操作在AQ的beforepost事件中实现,代码如下:stuid 是学生学号,per_name是学生姓名,per_card考勤卡号procedure TForm1.BitBtn2Click(Sender: TObject);//该段就是执行保存操作
    begin
    if aq.State in [dsinsert,dsedit] then
    begin
      try
        aq.Post;
      except
      end;
     end;
    end;procedure TForm1.AQBeforePost(DataSet: TDataSet);//执行更新操作
    var
      query: tadoquery;
      id:integer;
      strOldID,strOldName,strNewID,strNewName,stroldcard,strnewcard,strsql :string;
    begin
      if aq.State = dsinsert then
      begin
      try
      aqtmp.Insert;
      aqtmp.FieldByName('stuid').AsString := dbediteh2.Text;
      aqtmp.FieldByName('per_name').AsString := dbediteh1.Text;
      aqtmp.FieldByName('per_card').AsString := dbediteh3.Text;//考勤卡号赋值
      aqtmp.Post;
      except
      end;
      end;
      if aq.State=dsedit then
      begin
      with aqtmp do
      begin
     strOldID   := trim(FieldByName('stuid').AsString);
      strOldName := FieldByName('per_name').AsString;
     stroldcard:=FieldByName('per_card').asstring;  FieldByName('stuid').AsString   := dbediteh2.Text;
    FieldByName('name').AsString :=dbediteh1.Text;
     FieldByName('per_card').AsString :=dbediteh3.Text;
      Post;  end;
      strNewID   := trim(aqtmp.FieldByName('stuid').AsString);
     strNewName := aq.FieldByName('name').AsString;
     strnewcard:=  aq.fieldbyname('per_card').asstring;  if strOldID <> strNewID then
    begin
    with aqtmp do
    begin
      strSQL := 'Update tmp set stuid= ' + strNewID +
       ' Where stuid='+ strOldID;
       close;
       sql.Clear;
       sql.Text:=strsql;
       execsql;
    end;
    end;if strOldName <> strNewName then
    begin
    with aqtmp do 
    begin
      strSQL := 'Update tmp set per_name=' + QuotedStr(strNewName) +
        ' Where per_name = ' + QuotedStr(strOldName);
      close;
       sql.Clear;
       sql.Text:=strsql;
       execsql;
    end;    
      end;
    end;
    end;
      

  12.   

    我发现现在的原因是我在更新修改记录的时候,tmp表中对应的记录不能与学生表中的学生对应
    请问各位怎么才能使我在修改学生记录的时候,更新到tmp中时候怎么才能让更新的记录就是
    修改的记录啊
      

  13.   

    直接用sql更新就可以了
    update tmp set stuid='111',per_card='222',per_name='333'where stuid='xxx';