5-Minute-Snack: Exporting data using FireDAC

Happy New Year!

A couple of weeks ago I showed how to use FireDAC to import text data into datasets. Furthermore, Local SQL was used to filter and make other changes to the data.

Delphi also offers very simple means to export data from a query or dataset in a file.

The implementation can be found in the class TFDDataset . FireDAC tables and queries are derived from this class. Thus, we can use the following methods to export data from any of these:

  • SaveToFile : Allows to save the data in the dataset to a file. We have to specify a name with extension. Some of the extensions will determine the data format to be used. However, we can also specify the data format manually and use any extension we wish.
  • SaveToStream : Just like SaveToFile, but the content is written to a stream that we have to specify. Furthermore, the format has to be specified and is not obligatory.

screen-shot-2017-01-02-at-13-23-24

The file format specifier has to be of the type TFDStorageFormat :

sfAuto  Default, will pick format according to the file name you provide. Makes no sense for streams!
sfXML  Data will be in XML format
sfJSON  Data will be in JSON format
sfBinary  Data will be in proprietary binary format. FireDAC specific format. This format will definitely yield the smallest file size and will serve best if you want to import the data with another Delphi application that facilitates FireDAC. Be aware though that different Delphi versions might produce different binary files!

Let me point out the availability of JSON again. Even the documentation from Embarcadero misses to emphasize it. If you ever want to provide JSON data in a web service application, make use of TFDMemTable  and return the data using FireDAC’s JSON and XML capabilities. No need to do the work yourself. It comes in the box.

Providing a local backup of a data query in XML format becomes as easy as:

FireDAC will derive the format due to the xml file extension.

Of course, loading these files and streams can be done using the LoadFromFile  and LoadFromStream  methods of TFDMemTable .

Posted in Delphi Tagged with: , , , , , ,
0 comments on “5-Minute-Snack: Exporting data using FireDAC
1 Pings/Trackbacks for "5-Minute-Snack: Exporting data using FireDAC"
  1. […] how to quickly and easily export data from FireDAC to JSON and XML in RAD Studio. . https://flixengineering.com/archives/299 . #Android #iOS #macOS #Windows10 […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*