Skip to content

Instantly share code, notes, and snippets.

@moro
Created March 11, 2020 03:14
Show Gist options
  • Save moro/4b9c5449374a0f3d894d035ce0e50179 to your computer and use it in GitHub Desktop.
Save moro/4b9c5449374a0f3d894d035ce0e50179 to your computer and use it in GitHub Desktop.
思った以上に簡単にかけた便利コマンド
require 'csv'
require 'sequel'
def sql_to_csv(sql, db: Sequel.connect(ENV['DATABASE_URL']))
rel = db[sql]
CSV.generate('', force_quotes: ENV['NO_QUOTE'].nil?) do |csv|
csv << rel.columns
rel.each {|row| csv << row.values }
end
end
if $0 == __FILE__
puts sql_to_csv(ARGF.read)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment