Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'mysql'
- class Debsu
- def initialize
- @db = get_ARGV
- begin
- @dbh = Mysql.real_connect(@db[:host], @db[:user], @db[:pass], @db[:name])
- rescue Mysql::Error => e
- puts "Error: " + e
- exit
- end
- @db[:tables] = get_tables
- end
- def get_ARGV
- if ARGV.size < 4
- puts "Usage: ruby #{File.basename(__FILE__)} [hostname] [username] [password] [database]"
- exit
- end
- argv = { :host => ARGV[0], :user => ARGV[1], :pass => ARGV[2], :name => ARGV[3] }
- end
- def main
- show_tables
- show_table(get_table_from_user)
- @dbh.close
- end
- def show_table(table)
- puts table
- # to be continued..
- end
- def get_table_from_user
- print 'Pick table: '
- i = $stdin.gets.chomp.to_i
- return @db[:tables][i-1]
- end
- def show_tables
- puts 'Tables in database `' + @db[:name] + '`'
- @db[:tables].each_with_index do |table, i|
- puts "#{i+1}) `#{table}`"
- end
- end
- def get_tables # returns ["table1", table2", .. ]
- res = @dbh.query("SHOW TABLES")
- db_tables = []
- res.each do |row|
- db_tables << row.to_s
- end
- db_tables
- end
- end
Add Comment
Please, Sign In to add comment