Connect mysql:show tables

require “mysql”
dbh = Mysql.real_connect(“localhost”, “root”, “******”, “valuation”)
res = dbh.query(“show tables”)
puts res
res.free

the output is:
#Mysql::Result:0x997c014

in mysql console:

mysql> show tables;
±--------------------+
| Tables_in_valuation |
±--------------------+
| balance_sheet |
| test |
±--------------------+
2 rows in set (0.00 sec)

how can i get it with ruby?

Il 26/04/10 04.33, Pen T. ha scritto:

Is more simple than you think… i just rewrite your code with a little
“add”:

require “mysql”
dbh = Mysql.real_connect(“localhost”, “root”, “******”, “valuation”)
res = dbh.query(“show tables”)

  • puts res
  • puts “Tables_in_valutation”
  • res.each {|x| p x.to_s}
    res.free

Or…

require “mysql”
dbh = Mysql.real_connect(“localhost”, “root”, “******”, “valuation”)
res = dbh.query(“show tables”)

  • puts res
  • rows = []
  • puts “Tables_in_valutation”
  • res.each {|x| p x.to_s; rows<<x}
    res.free

require “mysql”
dbh = Mysql.real_connect(“localhost”, “root”, “******”, “valuation”)
res = dbh.query(“show tables”)

  • puts res
  • rows = []
  • puts “Tables_in_valutation”
  • res.each {|x| p x.to_s; rows<<x}
    res.free

You can take this one step further to actually print a table with
hirb, http://github.com/cldwalker/hirb:

require “mysql”
dbh = Mysql.real_connect(“localhost”, “root”, “******”, “valuation”)
res = dbh.query(“show tables”)
rows = []
res.each {|e| rows << e }
puts Hirb::Helpers::AutoTable.render(rows, :headers=>[“Tables in
valuation”])

With my own database this produces:
±--------------------+
| Tables in valuation |
±--------------------+
| nodes |
| schema_migrations |
| taggings |
| tags |
| trees |
| urls |
±--------------------+
6 rows in set

If you’re interested in having irb act as a database shell, try hirb
with one of
the database gems listed in
http://tagaholic.me/2010/03/11/hirb-and-tables-for-all.html

Gabriel

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs