-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql2csv.rb
38 lines (29 loc) · 895 Bytes
/
mysql2csv.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require 'mysql2';
require 'csv';
database = ARGV[0];
username = ARGV[1];
password = ARGV[2];
table = ARGV[3];
begin
client = Mysql2::Client.new(:host => "localhost", :username => username, :password => password)
client.query "USE #{database};"
# The metadata
metadataRS = client.query "SELECT GROUP_CONCAT(COLUMN_NAME SEPARATOR ',') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '#{database}' AN
D TABLE_NAME = '#{table}' GROUP BY TABLE_NAME;", :as => :array
columns = metadataRS.first[0].split(',')
# The contents...
rs = client.query "SELECT * FROM #{table}", :as => :array
data = CSV.generate do |csv|
csv << columns
rs.each do |row|
row.map { |v| v.nil? ? '' : v }
csv << row
end
end
puts data
rescue Mysql2::Error => e
puts e.errno
puts e.error
ensure
client.close if client
end