[cfgeeks] OK calling in the cavalry
Gilbert Young
gjyoung at cfl.rr.com
Tue Jan 2 00:29:46 EST 2007
On Jan 1, 2007, at 5:14 PM, Gilbert Young wrote:
>
> On Jan 1, 2007, at 10:24 AM, Steve Litt wrote:
>
>> On Monday 01 January 2007 02:30, Gilbert Young wrote:
>>> 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):
>>
>> Hi Gil,
>>
>> IIRC in Ruby == on two strings measures whether they reference the
>> same
>> variable, not whether their value is the same. There's some
>> string.equal() or
>> something like that, that does what you want. I'd look it up but
>> I'm about to
>> begin a 1200 mile car trip with my family of 5 from Chicago to
>> Orlando.
>>
>> SteveT
>
>
> Thanks Steve,
>
> That was partially it, I found the way to compare string to string,
> variable.equ?(variable2) returns either true or false. But then,
> I started getting this error:
>
> testthedb.rb:24:in `+': can't convert DBI::Row into String (TypeError)
>
> It appears the silent issue I was having was that I was trying to
> compare a string to a DBI::Row, and they would never match.
>
> Solution was to convert the variable that was a DBI::Row to a
> string using variable.to_s (variable.to_str did not work), and then
> compare:
>
> sth = dbh.execute("SHOW TABLES LIKE '#{ticker}'")
> tr = sth.fetch
> aaa = tr.to_s
> result = ticker.eql?(aaa)
> puts result
> if result
> puts "table #{ticker} exists"
> else
> puts "table #{ticker} does not exist"
> end
> sth.finish
>
>
> The latest run resulted in this:
>
> gilbert-youngs-powerbook-g4-12:~/projects/stock gjy$ ruby testthedb.rb
> Enter ticker symbol: lksdf
>
> false
> table lksdf does not exist
> gilbert-youngs-powerbook-g4-12:~/projects/stock gjy$ ruby testthedb.rb
> Enter ticker symbol: testtable
>
> true
> table testtable exists
> gilbert-youngs-powerbook-g4-12:~/projects/stock gjy$
>
> Now i'm going to try and pretty it up and comment the code so I
> don't forget what I had to do to get it to work :).
>
Well this is about as condensed as I can get the commands, anoyne
else see room to compress it any?
# SQL command to show tables that match $ticker
sth = dbh.execute("SHOW TABLES LIKE '#{ticker}'")
# Fetch the result of executing the SQL command, convert to string
tr = sth.fetch.to_s
# Compare the string received from the SQL command to the ticker
input by user
# if ticker = SQL output, print "table exists", if not, print "doesnt
exist"
if ticker.eql?(tr)
puts "table #{ticker} exists"
else
puts "table #{ticker} does not exist"
end
# It is finished
sth.finish
More information about the cfgeeks
mailing list