Skip to content

Instantly share code, notes, and snippets.

@Sutto
Forked from ocean/mysql-to-sqlite.rb
Created June 22, 2009 10:32
Show Gist options
  • Save Sutto/133910 to your computer and use it in GitHub Desktop.
Save Sutto/133910 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'rubygems'
require 'mysql'
require 'sqlite3'
def with_mysql
db_object = Mysql.init()
db_object.real_connect('localhost','things','1234','things')
begin
yield db_object
ensure
db_object.close
end
end
def with_sqlite
db_object = SQLite3::Database.new( "development.sqlite3" )
begin
yield db_object
ensure
db_object.close
end
end
with_mysql do |mysql|
results = mysql.query("select * from things order by ID limit 20, 3;") # get 3 for testing
results.each_hash do |row|
count_of_all+=1
rh = Hash.new
rh["person"] = row["Person"]
rh["place"] = row["Place"]
if row["Section"] then
rh["section"] = row["Section"]
end
if row["Regulation"] && row["Regulation"] != "Not Applicable" then
rh["regulation"] = row["Regulation"]
end
rh["description"] = row["Description"]
with_sqlite do |sqlite|
attributes = rh.to_a
place_holder = ["?"] * attributes.length
statement = "INSERT INTO things (#{attributes.map { |v| v[0] }.join(", ")}) VALUES (#{place_holder.join(", ")})"
sqlite.execute(statement, *attributes.map { |v| v[1] })
end
end
results.free
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment