[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Finding out if a Mysql database contains a certain table

ngw

2/19/2006 4:23:00 PM

Hi *,
I need to find out if a MySQL db contains a certain table, if not I have
to create it.
I'm using the SHOW TABLES instruction that returns a Mysql::Result
object, but after that I'm stuck, I don't know how to process the
resultset without writing awful code :p
I mean, I can resolve the problem by using a "state" variable inside a
block and turn it to true if it finds the table, but I guess there's a
more appropriate way to handle this kind of problems.

result = dbh.query("SELECT * FROM album")
foo = false
result.each {|row| foo = true if row.include?("Killing Joke")}
if foo == true
puts "HA!"
end
result.free

This is the code I have now, UGH !

TIA,
ngw
2 Answers

Caleb Tennis

2/19/2006 5:32:00 PM

0


On Feb 19, 2006, at 11:28 AM, ngw wrote:

> Hi *,
> I need to find out if a MySQL db contains a certain table, if not I
> have
> to create it.
> I'm using the SHOW TABLES instruction that returns a Mysql::Result
> object, but after that I'm stuck, I don't know how to process the
> resultset without writing awful code :p
> I mean, I can resolve the problem by using a "state" variable inside a
> block and turn it to true if it finds the table, but I guess there's a
> more appropriate way to handle this kind of problems.

A show tables command just returns a query of table names, 1 per row.

I'm not sure if you need to do something more complex, but in MySQL
it's valid to do:

CREATE TABLE IF NOT EXISTS

Caleb


David Vallner

2/19/2006 6:46:00 PM

0

Dna Nedela 19 Február 2006 18:31 Caleb Tennis napísal:
> A show tables command just returns a query of table names, 1 per row.
>

Sidenote: SHOW TABLES is a MySQLism, so watch out it you can't rule out the
data will be migrated to another DB.

David Vallner