DBF is a small, fast Ruby library for reading dBase, xBase, Clipper, and FoxPro database files.
DBF is a small, fast Ruby library for reading dBase, xBase, Clipper, and FoxPro database files.
DBF is a small, fast Ruby library for reading dBase, xBase, Clipper, and FoxPro database files.
DBF is tested to work with the following versions of Ruby:
Older Rubies are supported by older DBF release lines:
| DBF version | Ruby support |
|---|---|
| 5.2+ | 3.3+ |
| 4.3 – 5.1 | 3.1+ |
| 4.0 – 4.2 | 2.4+ |
| 3.x (3_stable) | 2.0 – 2.3 |
| 2.x (2_stable) | 1.8 – 1.9 |
DBF is Ractor-friendly: all library constants are deeply frozen, so tables can be opened, enumerated, and exported inside non-main Ractors.
Install the gem manually:
gem install dbfOr add to your Gemfile:
gem 'dbf'Open a DBF file using a path:
require 'dbf'
widgets = DBF::Table.new("widgets.dbf")Open a DBF file using an IO object:
data = File.open('widgets.dbf', 'rb')
widgets = DBF::Table.new(data)All errors raised by the library inherit from DBF::Error, so you can rescue the library as a unit:
begin
widgets = DBF::Table.new("widgets.dbf")
rescue DBF::Error => e
puts "Unable to read DBF file: #{e.message}"
endOpen a DBF by passing in raw data (wrap the raw data with StringIO):
widgets = DBF::Table.new(StringIO.new('raw binary data'))Use the block form to close the table automatically, like File.open:
DBF::Table.open('widgets.dbf') do |table|
table.each { |record| puts record.name }
endEnumerate all records
widgets.each do |record|
puts record.name
puts record.email
endFind a single record
widget = widgets.find(6)Note that find() will return nil if the requested record has been deleted and not yet pruned from the database.
The value for an attribute can be accessed via element reference in several ways.
widget.slot_number # underscored field name as method
widget["SlotNumber"] # original field name in dbf file
widget['slot_number'] # underscored field name string
widget[:slot_number] # underscored field name symbolGet a hash of all attributes. The keys are the original column names.
widget.attributes
# => {"Name" => "Thing1 | SlotNumber" => 1}Search for records using a simple hash format. Multiple search criteria are ANDed. Use the block form if the resulting record set is too big. Otherwise, all records are loaded into memory.
# find all records with slot_number equal to s42
widgets.find(:all, slot_number: 's42') do |widget|
# the record will be nil if deleted, but not yet pruned from the database
if widget
puts widget.serial_number
end
end
# find the first record with slot_number equal to s42
widgets.find :first, slot_number: 's42'
# find record number 10
widgets.find(10)DBF::Table is a Ruby Enumerable, so you get several traversal, search, and sort methods for free. For example, let's get only records created before January 1st, 2015:
…ruby require 'dbf'
contacts = DBF::Database::Foxpro.new('contact_database.dbc').contacts my_contact = contacts.record(1).spouses_interests
## dBase version compatibility
The basic dBase data types are generally supported well. Support for the
advanced data types in dBase V and FoxPro are still experimental or not
supported. If you have insight into how any of the unsupported data types are
implemented, please open an issue on Github. FoxBase/dBase II files are not supported
at this time.
### Supported data types by dBase version
| Version | Description | C | N | L | D | M | F | B | G | P | Y | T | I | V | X | @ | O | + |
|---------|-----------------------------------------------------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 02 | FoxBase | Y | Y | Y | Y | - | - | - | - | - | - | - | - | - | - | - | - | - |
| 03 | dBase III without memo file | Y | Y | Y | Y | - | - | - | - | - | - | - | - | - | - | - | - | - |
| 04 | dBase IV without memo file | Y | Y | Y | Y | - | - | - | - | - | - | - | - | - | - | - | - | - |
| 05 | dBase V without memo file | Y | Y | Y | Y | - | - | - | - | - | - | - | - | - | - | - | - | - |
| 07 | Visual Objects 1.x | Y | Y | Y | Y | - | - | - | - | - | - | - | - | - | - | - | - | - |
| 30 | Visual FoxPro | Y | Y | Y | Y | Y | Y | Y | Y | N | Y | Y | Y | N | N | N | N | - |
| 31 | Visual FoxPro with AutoIncrement | Y | Y | Y | Y | Y | Y | Y | Y | N | Y | Y | Y | N | N | N | N | N |
| 32 | Visual FoxPro with field type Varchar or Varbinary | Y | Y | Y | Y | Y | Y | Y | Y | N | Y | Y | Y | N | N | N | N | N |
| 7b | dBase IV with memo file | Y | Y | Y | Y | Y | Y | - | - | - | - | - | - | - | - | - | - | - |
| 83 | dBase III with memo file | Y | Y | Y | Y | Y | - | - | - | - | - | - | - | - | - | - | - | - |
| 87 | Visual Objects 1.x with memo file | Y | Y | Y | Y | Y | - | - | - | - | - | - | - | - | - | - | - | - |
| 8b | dBase IV with memo file | Y | Y | Y | Y | Y | - | - | - | - | - | - | - | - | N | - | - | - |
| 8e | dBase IV with SQL table | Y | Y | Y | Y | Y | - | - | - | - | - | - | - | - | N | - | - | - |
| f5 | FoxPro with memo file | Y | Y | Y | Y | Y | Y | Y | Y | N | Y | Y | Y | N | N | N | N | N |
| fb | FoxPro without memo file | Y | Y | Y | Y | - | Y | Y | Y | N | Y | Y | Y | N | N | N | N | N |
Data type descriptions
* C = Character
* N = Number
* L = Logical
* D = Date
* M = Memo
* F = Float
* B = Binary
* G = General
* P = Picture
* Y = Currency
* T = DateTime
* I = Integer
* V = VariField
* X = SQL compat
* @ = Timestamp
* O = Double
* + = Autoincrement
## Limitations
* DBF is read-only
* Index files are not utilized
## License
Copyright (c) 2006-2026 Keith Morrison >
Released under the MIT License. See [LICENSE](LICENSE) for the full text.No open issues yet, or sync has not completed.