Guest User

Untitled

a guest
Feb 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. require 'mysql'
  2.  
  3. class Debsu
  4. def initialize
  5. @db = get_ARGV
  6. begin
  7. @dbh = Mysql.real_connect(@db[:host], @db[:user], @db[:pass], @db[:name])
  8. rescue Mysql::Error => e
  9. puts "Error: " + e
  10. exit
  11. end
  12. @db[:tables] = get_tables
  13. end
  14.  
  15. def get_ARGV
  16. if ARGV.size < 4
  17. puts "Usage: ruby #{File.basename(__FILE__)} [hostname] [username] [password] [database]"
  18. exit
  19. end
  20. argv = { :host => ARGV[0], :user => ARGV[1], :pass => ARGV[2], :name => ARGV[3] }
  21. end
  22.  
  23. def main
  24. show_tables
  25. show_table(get_table_from_user)
  26. @dbh.close
  27. end
  28.  
  29. def show_table(table)
  30. puts table
  31. # to be continued..
  32. end
  33.  
  34. def get_table_from_user
  35. print 'Pick table: '
  36. i = $stdin.gets.chomp.to_i
  37. return @db[:tables][i-1]
  38. end
  39.  
  40. def show_tables
  41. puts 'Tables in database `' + @db[:name] + '`'
  42.  
  43. @db[:tables].each_with_index do |table, i|
  44. puts "#{i+1}) `#{table}`"
  45. end
  46. end
  47.  
  48. def get_tables # returns ["table1", table2", .. ]
  49. res = @dbh.query("SHOW TABLES")
  50. db_tables = []
  51. res.each do |row|
  52. db_tables << row.to_s
  53. end
  54. db_tables
  55. end
  56. end
Add Comment
Please, Sign In to add comment