Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
CodeDatabaseDelphiInterBase

How To Read And Write Array Field Types In PostgreSQL and InterBase

Did you know that InterBase and PostgreSQL both support a field type which is an array of values? It is not always obvious how to read and write to these database array field types. I get quite a lot of email from Delphi and C++ developers asking if I know how to achieve something they are struggling with. One such recent email was in connection with how to get database array fields to display and be updated correctly. I had to look into it because I was a little unsure myself but between the emailing developer and I we got it working perfectly. This article tells you how to do that with the power of Delphi and FireDAC along with some cool demo code you can download and use to experiment with arrays yourself. Note that array fields do not have wide support among DBMS. Neither MySQL nor SQL Server support them.

What are database array fields?

Database array fields in InterBase, as the name suggests, do not contain a single value but can contain a range of values. Those values all have to be of the same type such as integers, characters, and so on, but they can be multi-dimensional, not in the Star Trek sense, sadly, it just means you can have arrays like integer[4][128] which is an array of 4 lots of 128 integers. InterBase has supported array fields for many years and was one of the first databases to do so.

How to define an InterBase array field type

As you can see in the above IBConsole image, the field MY_ARRAY is an array field which contains 12 elements. It’s as simple as that in InterBase. The SQL to create the above table in InterBase is below.

I am going to use that table in a demo project to show a 12-month series bar chart graph using the truly excellent TChart component from Steema. You probably already have it installed in RAD Studio since we ship an Embarcadero version for free.

Why would I use a database array field?

Array fields are slightly counter-intuitive for relational database systems where the preference for techniques like database normalization leans heavily in the direction of tables with references to other tables. That form of having fields that are references which link to other tables is useful to simplify and abstract data but, sometimes, it can also introduce complexity and the need to use things like foreign keys or query joins and cascading updates. There are times when all you need is to have an array of values within the actual record itself, for example to depict a series of datapoints on a graph.

RAD Studio Delphi app showing a chart with data drawn from an InterBase database which uses array fields

Array fields can be used for far more than that, of course. They can be very useful for containing any types of data that are related to one another and where you want to have it all in one place, the actual record, rather than linked records. I have seen people use them for “other telephone numbers” fields, “previous password hashes” (to prevent reuse within N iterations), and for similar collections.

How do you define an array field type in PostgreSQL?

InterBase defines arrays in the way I showed above. For an array field type with 12 elements the range is defined as [1:12] so the first element is [1] and the last is [12]. For PostgreSQL arrays are similar except at the time of writing, despite having a syntax which allows determinate array subscripts PostgreSQL doesn’t enforce them and, in fact, ignores them so they behave as open arrays. Handy, but also a little wrong.

PostgreSQL array field subscripts are ignored

To define the array type in PostgreSQL you can use PGAdmin:

Defining an array field type in PGAdmin for PostgreSQL

The SQL for that is below. It creates an array field called the_minutiae which is an array of integer values.

I’m going to use this table in a demo project which will simulate displaying the “minutiae” of a person’s fingerprints. The minutiae are the unique characteristics of a fingerprint pattern like swirls with breaks in them, arches and so on. Fun fact: koalas have fingerprints which are almost indistinguishable from human ones. 🐨

How to read and update InterBase array fields?

The demo project shows you how, but here is a brief look at the details. I used FireDAC to create the connection and then an FDTable to manage the database table containing the array field.

FireDAC components reading and writing to an InterBase database table containing array fields

I added persistent fields, which makes it easier to link to visual controls using LiveBindings.

An InterBase array field
LiveBinings

How to write to an InterBase array field type?

Once I created those persistent fields it was very easy – here’s the code which runs when the user clicks on the “fill with test data” button.

There are other ways depending on your needs, but this is pretty fast. If you were going to update many thousands of records at once, then you need to consider writing queries which leverage things like ArrayDML and other techniques. My colleague, Stephen Ball wrote about it here: https://delphiaball.co.uk/2016/04/29/interbase-array-fields-firedac/

How to read an InterBase array field?

It you define the field as I described above, the actual Delphi code to read an InterBase array field is easy. Use the FieldValues property. Here’s a snippet from my example app. Note that although you define the array in InterBase as [1:12] the field index value in the code begins at zero. So, array value 1 is FieldValues[0].

How to read a PostgreSQL array field?

The easiest way to read a PostgreSQL array field is to use an interposing TFDMemtable. There are other ways to do it using some of PostgreSQL specific extensions to SQL DML but I used a FireDAC memory table in my example to show how it could be done.

How to read and write PostgreSQL array fields in Delphi

I created an FDTable, called ListTable (because it is going to drive the contents of the TListBox) which linked to the table in the database that contains the array field. I then created persistent fields the_person and the_minutiae. Then I added a FireDAC memory table and set the DataSetField property to point to the ListTable’s persistent the_minutiae field. This has the effect that every time the user or code moves the ListTable FDTable to a different record the memory table fills up with the array contents of the PostgreSQL array field. Cool huh? 😎

Then I can read the contents just like any regular FireDAC table:

How to write data to a table containing a PostgreSQL array field?

Once again, there is more than one way to write to a PostgreSQL array field. I chose to do it in my demo using a FireDAC query component and a SQL statement. It’s pretty easy. You could use the PostgreSQL-specific functions or FireDAC array DML. The query is a lot easier to understand though.

Where can I find full examples of reading or writing to InterBase array fields and reading or writing to PostgreSQL array fields?

I created two repositories with full working examples of how to read and write database tables which contain array fields.

The InterBase example is here: https://github.com/checkdigits/db_arrray_test

The PostgreSQL example is here: https://github.com/checkdigits/db_arrray_test_PostgreSQL


RAD Studio Logo

Why not download a free trial of RAD Studio today and see why we think it’s the fastest, easiest, and most efficient way to create cross platform apps that work on Windows, macOS, Linux, iOS and Android from a single code base?

The AI Codecamp: Learn, Code, Create See What's New in 12.2 Athens See What's New in 12.3 Athens

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Ian is the Embarcadero Developer Advocate, a professional writer, presenter, and host. He is a prolific software developer, voice actor, designer and poet. Ian is British American, born in London, now living in Dallas, Texas. "I get up early every day and write code".

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES