Add fields at runtime

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
lucascompa
Posts: 7
Joined: Mon 09 Jun 2008 07:49

Add fields at runtime

Post by lucascompa » Tue 05 Aug 2008 10:11

Hi.

I'm using the latest UniDac for MySQL and SQL Server with RAD 2007 Win32.

I've this problem:

I write a SQL.Text query in TUniQuery, like say:

select * from Customers order by name.

My program at runtime executes the query and TUniQuery automatically creates the fields corresponding to the Customers table.

Now I want to add a calculated (or in-memory) field to the TUniQuery.

The istruction Fields.Add raise an exception saying that this operation is not allowed with an open dataset.

So I tried to create the calculated field before opening the query.
But in this way when I open the TUniQuery, it does not automatically create the fields of the Customer's table.

So after beeing opened, the TuniQuery has only one field, the calculated one.

How can I solve this problem?

Thanks

NOTE: I cannot know the fields of the table Customers before have opened it.

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Wed 06 Aug 2008 14:38

To create the fields of your SQL query, use the following code:

Code: Select all

  UniQuery.FieldDefs.Update;
  for i := 0 to UniQuery.FieldDefList.Count - 1 do
    with UniQuery.FieldDefList[i] do
      if (DataType  ftUnknown) and not (DataType in ObjectFieldTypes) then
        CreateField(UniQuery, nil, UniQuery.FieldDefList.Strings[i]);
After that add calculated fields and then open the query.

lucascompa
Posts: 7
Joined: Mon 09 Jun 2008 07:49

Post by lucascompa » Fri 08 Aug 2008 08:32

Thanks for your reply.

It does what I need.

But, how can the command

Code: Select all

UniQuery.FieldDefs.Update;
get the list of fields?

Does it send the query to MySQL?

And after, when I open the query, does it send the query again?

PS.

Can I add non calculated field?

For example with a Customers table with phisical columns:
"code, name"
can then Uniquery component keep in memory a field "State"?

I use this field for an elaboration on the customers table. I write there an information that I reuse, but it is not phisically on the database.

Thanks

Post Reply