Monday, August 7, 2006

Using the Perl Xbase module to read xBase databases

I recently had occasion to read some xBase database files (e.g. dBase or FoxPro files). What I was doing needed some of the capabilities of Perl. I found a module that reads xBase databases directly: Xbase.

Here is a simple set of code to read all of the records in an xBase database:

use strict;
use Xbase;

# Create a new Xbase object
my $database = new Xbase;

# Open the database
my $db_name = 'C:exampledatabase.dbf';

# Get the last record number
my $end=$database->lastrec;

# Go to the first record

# Loop through the records
for my $i (1..$end)
# Print the record number
print $database->recno . ". ";

# Get the fields in this record
my @fields = $database->get_record;

# Loop through the fields
for my $f (0..$#fields)
# Print the values of each field
print $fields[$f] . "\t";
print "\n";

# Go to the next record

# Print information about the database,
# including the names of the fields
print $database->dbf_type . "\n";
print "Last update: " . $database->last_update . "\n";
print $database->dbf_stat;

# Close the database

This code is adapted from the perldoc documentation of the Xbase module.


  1. Hola, tendras mas ejemplos como busquedas sobre xBase con Perl? no encuentro mucha documentacion al respecto.

  2. Hola, Leonel,
    Lo que he escrito aquí es mas o menos todo lo que yo se del modulo XBase. Hay algo en particular que quieres hacer con el? Hay una pagina MAN aqui:
    Buena suerte; escríbeme otra vez si tienes mas preguntas.

  3. The one thing I can't find, no matter how hard I look, is how to insert new records into a Dbase table with the Xbase module.


I moderate comments blog posts over 14 days old. This keeps a lot of spam away. I generally am all right about moderating. Thanks for understanding.