Thursday, December 19, 2024
HomeProductsDelphi DACHow to Access DBF Databases in Delphi

How to Access DBF Databases in Delphi

xBase is the generic term for all databases deriving from the original dBase database format (.dbf). The list includes Visual FoxPro, Clipper, dBase III, dBase IV, and many others. These databases are informally known as dBase clones because they are either direct descendants of dBase or mimick it.

xBase databases store large amounts of formatted data in a structured form in .dbf data files. In dBase-like databases, a .dbf file stores a single table where the table description, field descriptors, and records are kept. Modern dBase-like databases also have files for large text fields (memos), commands, procedures, backups, etc.

There are various database engines that can read and manipulate data in DBF files, but none of them understands all formats of xBase databases – most of these database  engines can interact with one or two dialects of the xBase family. Unlike other existing solutions, the Delphi data access provider TDBFUniProvider in UniDAC provides an engine that understands DBF files across many dBase-like databases.

TDBFUniProvider provides direct access to xBase databases and supports all dBase native data types (character, numeric, logical, data, memo). It serves as a SQL engine that executes your commands against database files. The Delphi code in your project is compiled into an executable file that doesn’t need any other external files to access and manipulate data in DBF files.

UniDAC Delphi component for accessing xBase database files supports multiple database file formats: dBase III, dBase IV, dBase V, dBase VII, FoxPro2, Visual FoxPro, Clipper, CodeBase, HiPer-Six. Besides allowing a developer to use the standard SQL-92 syntax, it offers a fast way to rebuild a table and remove deleted records to reduce the database file size.

Creating a Sample Delphi App to Access xBase Databases

We’ll create a simple Delphi application that will connect to a database in Visual FoxPro format and display records from a table when you click the Display button.

1. Find the TUniConnection, TUniQuery, TUniDataSource, TDBFUniProvider, TDBGrid and TButton components in the Tool Palette and drop them on the form.

Delphi Form for DBF database

2. Double-click the UniConnection1 component on the form. Switch to the Options tab and set Direct to True.

DBF Delphi Direct Connection

3. Go back to the Connect tab, choose DBF as Provider and enter the path to Visual FoxPro (or any other xBase database) on your machine. Click Connect. If everything goes well, the red circle will become green.

Establishing connection to Visual FoxPro

4. In UniDataSource1, set the DataSet property to UniQuery1.

DBF Delphi DataSource Datasets

5. Select DBGrid1 and set the DataSource property to UniDataSource1.

DBF Delphi DataSource

6. Choose the UniQuery1 component and set the Connection property to UniConnection1, then double-click the component and enter your SQL statement. Click OK.

DBF Delphi SQL Statement

7. Change the Caption property of the button to Display in Object Inspector. Double-click the button, switch to the Code tab and add UniQuery1.Open; to the OnClick event handler code.

DBF Delphi OnClick Event

9.  Press F9 to compile and run your application. If the program compiles without the errors, you should see the compiled form application.  Click Display to fetch and view the data in your Visual FoxPro table.

DBF Delphi Retrieve Table

Retrieval of Corrupted Data and Metadata

xBase dialects have a long history, and situations where fields in .dbf files contain data of unsupported type are not so rare. To resolve any issues with unsupported data types, UniDAC provides two options: IgnoreDataErrors and IgnoreMetaDataErrors. The former option forces UniDAC to ignore corrupted data errors when opening a DBF table, and the latter forces UniDAC to ignore metadata errors:  when both options are set to True, corrupted data will be skipped, and other data will be properly retrieved.

Another feature is the ability to automatically determine the dialect of an xBase database when you don’t exactly know the format of your DBF files – the dfAuto value in the DBFFormat option.

Supported Target Platforms

UniDAC supports multiple target platforms: you can create an application that accesses DBF databases for:

  • Windows, 32-bit and 64-bit
  • macOS, 32-bit and 64-bit
  • iOS, 32-bit and 64-bit
  • Android, 32-bit and 64-bit
  • Linux, 32-bit and 64-bit
RELATED ARTICLES

Whitepaper

Social

Topics

Products