TUniQuery - SQLInsert.

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
lcdk
Posts: 4
Joined: Sun 24 Nov 2019 22:37

TUniQuery - SQLInsert.

Post by lcdk » Sun 24 Nov 2019 22:43

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;
  

FCS
Posts: 176
Joined: Sat 23 Feb 2013 18:46

Re: TUniQuery - SQLInsert.

Post by FCS » Mon 25 Nov 2019 19:59

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

MaximG
Devart Team
Posts: 1822
Joined: Mon 06 Jul 2015 11:34

Re: TUniQuery - SQLInsert.

Post by MaximG » Mon 02 Dec 2019 13:57

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

Post Reply