The GeoLite Country database at:
http://www.maxmind.com/app/geolitecountry)
is just fine for countries. The Ruby gem works nicely:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/...
... and there are other APIs as well:
http://www.maxmind.com/app/api
MaxMind is the standard.