[cfgeeks] OK calling in the cavalry
Gilbert Young
gjyoung at cfl.rr.com
Mon Jan 1 02:30:38 EST 2007
Ruby Q. I am trying to compare two variables that are strings, one
taken from a SQL command, one taken from user input earlier (ticker):
snippet:
------------------
sth = dbh.execute("SHOW TABLES LIKE '#{ticker}'")
tr = sth.fetch
puts tr
if tr == ticker
puts "table #{ticker} exists"
else
puts "table #{ticker} does not exist"
end
puts tr
sth.finish
------------------
end snippet
seems like the "if tr == ticker" line isn't doing like I wish it to,
and is doing exactly like I am telling it to, which i'm not sure what
that is. I have tested this and tr and ticker do appear to print out
exactly the same. no matter what I type, either a table I know to
exist or one that I know not to exist, it consistently prints out
"table XYZ does not exist"
Below is the output of two test runs:
-----------------
gilbert-youngs-powerbook-g4-12:~/projects/stock gjy$ ruby testthedb.rb
Enter ticker symbol: sdgf
nil
table sdgf does not exist
nil
gilbert-youngs-powerbook-g4-12:~/projects/stock gjy$ ruby testthedb.rb
Enter ticker symbol: testtable
testtable
table testtable does not exist
testtable
gilbert-youngs-powerbook-g4-12:~/projects/stock gjy$
-----------------
Eventually I will have other logic in there that will either create
or append any needed data to a table. But right now i'd just like to
see the logic work :).
OBTW this is the most graceful way I have found in ruby to determine
whether a table exists or not, and, to (attempt to) execute logic
based on if it exists or not.
The above has the gems mysql and dbi in use, fwiw.
TIA,
Gil
More information about the cfgeeks
mailing list