Created
September 30, 2011 09:36
-
-
Save pjotrp/1253261 to your computer and use it in GitHub Desktop.
Ruby snippet to convert Foxpro file to SQLite or MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good , but that changes should be made to convert a. sql?