KeyFields Problem

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
yucelkarapinar
Posts: 2
Joined: Wed 02 Aug 2017 09:20

KeyFields Problem

Post by yucelkarapinar » Wed 02 Aug 2017 09:23

I have a table with fields "field_1, field_2, field_3". Primary index is "field_1;field_2"

Unitable1.Options.StrictUpdate := False;
Unitable1.IndexFieldNames := "field_1;field_2"
Unitable1.KeyFields := "field_1"

For example I have 5 records in the table of field_1 column has values "x", field_2 values are different.

When I change the value of any cell in field_3 column with field_1 "x" value, all the cells in field_3 values changes.

When I delete one of the records that field_1 value is "x", all records is deleted too where field_1 value is "x".

When I try to change field_2 value with field_1 "x", it gives "Duplicate key" error.

JensVitec
Posts: 9
Joined: Tue 25 Jul 2017 08:01

Re: KeyFields Problem

Post by JensVitec » Wed 02 Aug 2017 11:15

You need to set Unitable1.KeyFields := 'Field_1;Field_2'
Setting the KeyFields property is telling UniTable which field is in fact the primary key for its DML.

So you are sending
Update table set fields..
WHERE
FIELD_1 = value

yucelkarapinar
Posts: 2
Joined: Wed 02 Aug 2017 09:20

Re: KeyFields Problem

Post by yucelkarapinar » Wed 02 Aug 2017 14:21

I need to write Field_1 to "Keyfield's" in order to be able to make search according to Field_1 area by using FindKey.
I can perform search with "Locate" too, but in the project which I'm converting from Paradox to SQL, there are lots of FindKey commands which are used that way.
Is there anything else I can do except using Locate Command?

azyk
Devart Team
Posts: 1119
Joined: Fri 11 Apr 2014 11:47
Location: Alpha Centauri A

Re: KeyFields Problem

Post by azyk » Fri 04 Aug 2017 09:38

One of the variants to solve this task can be the use of custom UPDATE/DELETE SQL queries. For example, if necessary
yucelkarapinar wrote: When I change the value of any cell in field_3 column with field_1 "x" value, all the cells in field_3 values changes.
Then you can compose such a SQL query to edit the data:

Code: Select all

IF (:field_3 <> :Old_field_3) AND (:field_1 = 'x')
UPDATE table_11 SET field_3 = :field_3
And set the generated SQL query to the TUniTable.SQLUpdate.Text property:

Code: Select all

  Unitable1.Options.StrictUpdate := False;
  UniTable1.SQLUpdate.Text := ' IF (:field_3 <> :Old_field_3) AND (:field_1 = ''x'') ' +
                              '  UPDATE table_11 SET field_3 = :field_3';
  Unitable1.Open;
For more information about SQLUpdate in our online documentation:
https://www.devart.com/unidac/docs/?dev ... update.htm

Post Reply