Skip to content

Instantly share code, notes, and snippets.

@pjotrp
Created September 30, 2011 09:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pjotrp/1253261 to your computer and use it in GitHub Desktop.
Save pjotrp/1253261 to your computer and use it in GitHub Desktop.
Ruby snippet to convert Foxpro file to SQLite or MySQL
# Ruby script to convert Foxpro file to SQLite or MySQL
#
USE_SQLITE3 = false
require 'rubygems'
require 'dbf'
require 'active_record'
if USE_SQLITE3
require 'sqlite3'
MY_DB_NAME = "sqlite.db"
MY_DB = SQLite3::Database.new(MY_DB_NAME)
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => MY_DB_NAME)
else
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "updates",
:password => 'mydbparrot'
)
end
class Update < ActiveRecord::Base; end
# do a quick pseudo migration. This should only get executed on the first run
if !Update.table_exists?
table = DBF::Table.new("updates.dbf")
eval(table.schema) # this updates the schema!
print table.schema
Update.reset_column_information
table.each_with_index do |record, i|
print "." if i%1000 == 0 # show progress
# We need to reduce the extra mapping that record.attributes
# provides...
select = record.attributes.find_all { | n | n[0] =~ /^[a-z]/ }
m = {}
select.each do | n |
m[n[0]] = n[1];
end
Update.create(m) # go ahead and add the record!
end
else
$stderr.print "Database updates already exists!"
end
@raulsalinas
Copy link

good , but that changes should be made to convert a. sql?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment