# Connect mysql:show tables

**URL:** <https://www.ruby-forum.com/t/connect-mysql-show-tables/187570>\
**Category:** Ruby\
**Created:** [April 26, 2010, 4:33am UTC](https://www.ruby-forum.com/t/connect-mysql-show-tables/187570 "2010-04-26T04:33:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [April 26, 2010, 4:33am UTC](https://www.ruby-forum.com/t/connect-mysql-show-tables/187570/1 "2010-04-26T04:33:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [April 26, 2010, 11:45am UTC](https://www.ruby-forum.com/t/connect-mysql-show-tables/187570/2 "2010-04-26T11:45:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [April 26, 2010, 10:55pm UTC](https://www.ruby-forum.com/t/connect-mysql-show-tables/187570/3 "2010-04-26T22:55:44Z")

</div>

> 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:](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

> **[Tagaholic - Hirb - And Tables for All](http://tagaholic.me/2010/03/11/hirb-and-tables-for-all.html)**
>
> Almost a year ago, hirb started as an itch to get mysql-like tables for Rails' ActiveRecord models. Now, hirb 0.3.0 provides table views for ten additional database gems. Whether you use hirb w...

Gabriel
