Page 1 of 1

TUniQuery - SQLInsert.

Posted: Sun 24 Nov 2019 22:43
by lcdk
I give error message "DataSet not in edit or insert mode." What am i doing wrong.

Code: Select all

Query := TUniQuery.Create(nil);
  try
    Query.Connection := DataConnect.Connection;
    Query.SQLInsert.Text := 'INSERT INTO ' + Tab_Company_bank + '(' + comba_nraccount + ',' + comba_namebank + ',' + comba_currency + ',' + comba_swift + ',' + comba_iban +
      ') VALUES (:b1,:b2,:b3,:b4,:b5)';
    Query.SQLUpdate.Text := 'UPDATE ' + Tab_Company_bank + ' SET ' + comba_nraccount + '=:b1,' + comba_namebank + '=:b2,' + comba_currency + '=:b3,' + comba_swift + '=:b4,' +
      comba_iban + '=:b5 WHERE ' + comba_id + '=' + inttostr(fid);
    Query.Params.CreateParam(ftString,'b1',ptUnknown).Value := edtAccount.Text;
    Query.Params.CreateParam(ftString,'b2',ptUnknown).Value := edtBankName.Text;
    Query.Params.CreateParam(ftString,'b3',ptUnknown).Value := edtCurrency.Text;
    Query.Params.CreateParam(ftString,'b4',ptUnknown).Value := edtSWIFT.Text;
    Query.Params.CreateParam(ftString,'b5',ptUnknown).Value := edtIBAN.Text;
    if fid = 0 then
      Query.Post //Query.Insert
    else
      Query.UpdateRecord;
    close;
  finally
    Query.Free;
  end;
  

Re: TUniQuery - SQLInsert.

Posted: Mon 25 Nov 2019 19:59
by FCS
Hi,
You should use:
- Open/Execute method
- Query.Insert or/and Query.Edit

before calling the Post method.

Using parameters you should use Prepare method too.

In your code the Query is not opened.

The SQLs for Insert and Update are designed for any specific tasks different to default.

You can try this:

Query.SQL.Clear;
Query.SQL.Add('SELECT comba_nraccount , comba_namebank, etc FROM Tab_Company_bank WHERE .....');
Query.SQL.Open;
Query.SQL.Insert;
Query.FieldByName('comba_nraccount').AsString/Integer/... := 'abcd';
Query.FieldByName(.....).AsString/Integer/... := 'abcd';
Query.SQL.Post;

Regards

Re: TUniQuery - SQLInsert.

Posted: Mon 02 Dec 2019 13:57
by MaximG
You can refer to our documentation for a detailed description of the features you're interested in:

https://www.devart.com/unidac/docs/deva ... insert.htm
https://www.devart.com/unidac/docs/deva ... update.htm