delphi fdquery mysql在循环中插入

时间:2018-05-13 10:59:35

标签: delphi

我必须插入一个循环。 在我从其他数据库读取时,我获取数据并创建我的插入查询,如此

 form2.FDQuery4.First;
 while (not form2.FDQuery4.Eof) do begin
   try
     form2.FDQuery5.SQL.Text := 'INSERT INTO `tm_frame_data_val` (`frame_data_id`, `value`, `act_date`) VALUES ( :fdida, :value, :acdoo );';
     form2.FDQuery5.Command.CommandKind := skInsert;

          form2.FDQuery5.ParamByName('fdida').Value := '1';     //form2.FDQuery4.FieldByName('tfdid').AsString
          form2.FDQuery5.ParamByName('value').Value := '1';    // TmFielsDataVal
          form2.FDQuery5.ParamByName('acdoo').Value := '1';  //tmSentTimeStamp.ToString

     form2.FDQuery5.Execute;

   finally
     form2.ListBox3.Items.Add( 'error on writing to db' );
   end;
  form2.FDQuery4.Next;
 end;

在我的循环中我收到了这个错误

enter image description here

我使用ExecuteOpenOrExecuteExecSQL购买时始终获得此错误。 为什么在插入查询中发生这种情况!?

当我使用tryfinally时,为什么它会在showmessage显示错误!?

1 个答案:

答案 0 :(得分:0)

请尝试以下一项或两项:

1)在ExecSQL之前分配CommandKind:= skInsert:

class ReservationViewSet(viewsets.ModelViewSet):
    queryset = models.Reservation.objects.all()
    serializer_class = serializers.ReservationSerializer

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)

2)使用OpenOrExecute而不是ExecSQL:

TypeError at /reservations_manager/reservations/
Got a `TypeError` when calling `Reservation.objects.create()`.
This may be because you have a writable field on the serializer class that is not a valid argument to `Reservation.objects.create()`.
You may need to make the field read-only, or override the ReservationSerializer.create() method to handle this correctly.

这些都没有经过我的测试,但是documentation(参见“Q3”)似乎表明这将是您的解决方案。