connect mysql:show tables

P

Pen Ttt

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?
 
F

Francesco Vollero

Il 26/04/10 04.33, Pen Ttt ha scritto:
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?

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
 
G

ghorner

   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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top