Posted on August 1, 2006 at 6:21 pm
The DBF library project page is up:
http://rubyforge.org/projects/dbf/
There’s no documentation yet, but you should be able to figure out how to use it by looking at the unit tests.
You can download it from the project page as a gem or tar.gz or install it via rubygems:
gem install dbf



I am definitely a Ruby-noob, so this may be bonehead simple, but. . .
When I run
require "DBF"
@dbf = DBF::Reader.new("Driveway.dbf")
recs = @dbf.record(1)
p recs["SHAPE"]
it returns “circular”, which is expected from the data.
However, looking at the unit tests, it appears I should be able to run
require "DBF"
@dbf = DBF::Reader.new("Driveway.dbf")
rec = @dbf.record(1)
p rec["SHAPE"]
@dbf.records.each do |record|
p record['SHAPE']
end
but what results is:
>ruby scr.rb
"circular"
c:/ruby/lib/ruby/gems/1.8/gems/dbf-0.1.0/lib/dbf/reader.rb:55:in `unpack_string': undefined method `unpack' for nil:NilClass (NoMethodError)
from c:/ruby/lib/ruby/gems/1.8/gems/dbf-0.1.0/lib/dbf/reader.rb:75:in `build_record'
from c:/ruby/lib/ruby/gems/1.8/gems/dbf-0.1.0/lib/dbf/reader.rb:72:in `each'
from c:/ruby/lib/ruby/gems/1.8/gems/dbf-0.1.0/lib/dbf/reader.rb:72:in `build_record'
from c:/ruby/lib/ruby/gems/1.8/gems/dbf-0.1.0/lib/dbf/reader.rb:60:in `records'
from c:/ruby/lib/ruby/gems/1.8/gems/dbf-0.1.0/lib/dbf/reader.rb:60:in `initialize'
from c:/ruby/lib/ruby/gems/1.8/gems/dbf-0.1.0/lib/dbf/reader.rb:60:in `new'
from c:/ruby/lib/ruby/gems/1.8/gems/dbf-0.1.0/lib/dbf/reader.rb:60:in `records'
from scr.rb:5
>Exit code: 1
It’s a .dbf from an ESRI shapefile, which is fairly straightforward .dbf format (for ESRI).
If it’s page-slap simple, I won’t be surprised, but I am stuck.
Ron
Hi Ron,
You’ve got the syntax right. I think the problem is with the DBF library. If you send me the DBF file, I’ll sort out the problem and push out a new version of the gem. You can email it to me at keithm@infused.org.
Keith
The new version does just what I hoped!
Thanks again,
Ron
Hi,
Very useful library ! I’ve just a trouble with MEMO fields in files I received (don’t know exactly from where they’re come).
I’ve a DBF file with his friend to store memo : the DBT file. But in you library your search a FPT file (not a DBT). I tried to rename to try it, but memo will not be retrieved.
Is DBT files supported ?
Sam.
Hello, Keith! You do great job! Thanks!
Sorry for my english. But, please, check file reader.rb. Line 111 method get_field_descriptors:
You write:
…. Field.new(*@data_file.read(32).unpack(’a10xax4cc’))}
But, can be was write more correctly:
‘a10xax4CC’
Please, check it. Size of field always must be more 0.
With your code, if length of a field equals 150, then function “unpack” returns value “-106″ (negative!)
And more … I had difficulties with reading files which contain the deleted records.
It seems to me, that it is necessary to reconsider a code of functions “records” and “record(index)”. For elimination of problems I rewrite a code as follows:
def records
seek_to_record(0)
@records ||= Array.new(@record_count) {|i| (active_record?) ? build_record : seek_to_record(i+1)}
end
...
def record(index)
seek_to_record(index)
(active_record?) ? build_record : nil
end
Or more correctly:
def records
seek_to_record(0)
@records ||= Array.new(@record_count) do |i|
if active_record?
build_record
else
seek_to_record(i+1)
nil
end
end
end
x0n,
Good fixes! Your patches are now in the subversion trunk. I’ll be releasing a new version in a day or two.
Thanks,
Keith
I just released version 0.4.0 which includes the fixes described above as well as support for dBase III memo files.
I still need a dBase IV database (with memo) file for testing. If anybody’s got one, please send it my way!
Thanks
Keith
I just tried the 0.4 version, wich can extract memo from my DBT files. It runs very well, good job !
Thanks.
hello i know this is not good place to ask this question but i m facing problem in using DBF gem
I am using dbf gem for my application I am using windows for the development.
When I try to read the dbf file using
require ‘dbf’
table = DBF::Table.new(”#{RAILS_ROOT}/db/0809/accgroup.dbf”, :in_memory => false)
it gives me permission denied error.
Please tell me how I can resolve this problem
This problem is coming with all the dbf files not the specific file.
Thanks
Arun: It works fine for me under Windows XP, so I’m not sure what the issue is.