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
12 Comments:
Posted on August 8, 2006 at 4:16 am by Ron Phillips

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

Posted on August 8, 2006 at 9:59 am by Admin

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

Posted on August 9, 2006 at 3:47 am by Ron Phillips

The new version does just what I hoped!

Thanks again,
Ron

Posted on October 2, 2006 at 7:46 am by Sam @ work

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.

Posted on October 4, 2006 at 2:52 am by x0n

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!)

Posted on October 4, 2006 at 4:29 am by x0n

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

Posted on October 4, 2006 at 4:58 am by x0n

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

Posted on October 4, 2006 at 10:55 am by Keith

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

Posted on October 7, 2006 at 12:24 pm by 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

Posted on October 9, 2006 at 1:46 am by Sam @ work

I just tried the 0.4 version, wich can extract memo from my DBT files. It runs very well, good job !

Thanks.

Posted on October 7, 2008 at 12:09 am by Arun Agrawal

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

Posted on October 7, 2008 at 10:37 am by Keith

Arun: It works fine for me under Windows XP, so I’m not sure what the issue is.

Leave a comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>