Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

dbf

> 数据库
Open source

DBF is a small, fast Ruby library for reading dBase, xBase, Clipper, and FoxPro database files.

281 stars0 likes1 views
WebsiteGitHub

About

DBF is a small, fast Ruby library for reading dBase, xBase, Clipper, and FoxPro database files.

DBF

DBF is a small, fast Ruby library for reading dBase, xBase, Clipper, and FoxPro database files.

  • Project page:
  • API Documentation:
  • Report bugs:
  • Questions: Email and put DBF somewhere in the subject line
  • Change log:

Compatibility

DBF is tested to work with the following versions of Ruby:

  • Ruby 3.3.x, 3.4.x, 4.0.x

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.

Installation

Install the gem manually:

ruby
gem install dbf

Or add to your Gemfile:

ruby
gem 'dbf'

Basic Usage

Open a DBF file using a path:

ruby
require 'dbf'
widgets = DBF::Table.new("widgets.dbf")

Open a DBF file using an IO object:

ruby
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:

ruby
begin
  widgets = DBF::Table.new("widgets.dbf")
rescue DBF::Error => e
  puts "Unable to read DBF file: #{e.message}"
end

Open a DBF by passing in raw data (wrap the raw data with StringIO):

ruby
widgets = DBF::Table.new(StringIO.new('raw binary data'))

Use the block form to close the table automatically, like File.open:

ruby
DBF::Table.open('widgets.dbf') do |table|
  table.each { |record| puts record.name }
end

Enumerate all records

ruby
widgets.each do |record|
  puts record.name
  puts record.email
end

Find a single record

ruby
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.

ruby
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 symbol

Get a hash of all attributes. The keys are the original column names.

ruby
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.

ruby
# 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)

Enumeration

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.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubydatabasedbasedbffoxpro

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库