Finding out if a Mysql database contains a certain table

N

ngw

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
 
C

Caleb Tennis

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
 
D

David Vallner

D=C5=88a Nede=C4=BEa 19 Febru=C3=A1r 2006 18:31 Caleb Tennis nap=C3=ADsal:
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=
=20
data will be migrated to another DB.

David Vallner
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top