insert or ignore

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
sandy771
Posts: 194
Joined: Tue 22 May 2007 13:57

insert or ignore

Post by sandy771 » Wed 11 Dec 2013 23:49

I am inserting a large number of records into a database 1000 at a time within a transaction using

UniQuery->Append();
...
UniQuery->Post()

I am using UniQuery->FieldByName(...) as a few of the fields are largeish blobs

One of the fields should contain unique values and has a unique constraint and I want to use the equivalent of INSERT OR IGNORE.

Is there an easy way to go about this. At the moment I am inserting all of the rows and thende-duplicating, which I expect is slower than it could be. I expect that there are better was of doing this.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: insert or ignore

Post by AlexP » Fri 13 Dec 2013 13:08

Hello,

If your database supports the INSERT OR IGNORE construct, you can set your own INSERT query with this construct in the UniQuery.SQLInsert property. If the DB doesn't support this functionality, you can use the EDAError.IsKeyViolation property in the following way:

Code: Select all

begin
  UniQuery1.Append;
  UniQuery1.FieldByName('DEPTNO').AsInteger := 40;
  UniQuery1.Post;
  UniQuery1.Append;
  UniQuery1.FieldByName('DEPTNO').AsInteger := 40;
  try
    UniQuery1.Post;
  Except
    on e: EUniError do
      if not EDAError(E).IsKeyViolation then
        Raise
      else
        UniQuery1.Cancel;
  end;
end;

sandy771
Posts: 194
Joined: Tue 22 May 2007 13:57

Re: insert or ignore

Post by sandy771 » Fri 13 Dec 2013 14:47

Thanks Alex

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: insert or ignore

Post by AlexP » Fri 13 Dec 2013 15:08

Hello,

If you have any other questions, feel free to contact us.

sandy771
Posts: 194
Joined: Tue 22 May 2007 13:57

Re: insert or ignore

Post by sandy771 » Mon 16 Dec 2013 12:58

Hi Alex

Is there a memthod of doing an insert or ignore when loading a table via a tuniloader?

Cheers
Paul

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: insert or ignore

Post by AlexP » Mon 16 Dec 2013 14:47

Hello,

Unfortunately, for the time being, there is no such capability when using UniLoader. We will consider the possibility to add an onError event to UniLoader in one of the next versions.

Post Reply