I am trying to insert a line of data into a table in MySQL 5, the data I
am using is:
StartTime: 20090413 13:41:53.000 TestName:changehistory.pl Pass:20
Fail:0 Total:20
Using this function I strip the data down and am going to insert it
using the ActiveRecord insert function (I’m not using Rails as this is a
Ruby app) so I am loading the ActiveRecord library and using it
directly.
class AutoHarnesses < ActiveRecord::Base
end
def test_add(line,name)
@machine = name[2]
begin
# Test Results will have data in the line
if line =~ /data/
# Scan the line and pull out the pairs we want
# for eventual sql insert, thank you ruby-forums!
s = line.scan(/(\w+):\s*([\s\d:.]+|\S+)/)
s.shift
# Assign values
s.each { |key,value|
if key == "TestName"
then @testname = value
elsif key == "Pass"
then @passes = value.to_i
elsif key == "Fail"
then @failures = value.to_i
elsif key == "Total"
then @totals = value.to_i
else
LOGGER.warn "Did not find a recognizable test
result."
end
}
agNew = AutoHarnesses.connection.insert(“INSERT into
auto_harnesses
(machinename,testname,test_pass,test_fail,
test_total) VALUES (”{@machine}","{@testname}",
“{@passes}”, “{@failures}”, “{@totals}”)")
end
rescue
# Print out errors
LOGGER.warn $!
end
end
If I run some debugging statements after assinging the values I get:
Test changehistory.pl on testbox20 had 20 passes and 0 fails from a
total of 20. So I know the values are being set properly.
The data looks ok, but the insert returns the following error:
#<ActiveRecord::StatementInvalid: Mysql::Error: Incorrect integer value:
‘{@passes}’ for column ‘test_pass’ at row 1: INSERT into
auto_harnesses
(machinename,testname,test_pass,test_fail,
test_total) VALUES ("{@machine}","{@testname}",
“{@passes}”, “{@failures}”, “{@totals}”)>
I thought converting the Pass, Fail and Total values to_i would be best,
since those database columns are int, but that does not seem to resolve
anything. I thought maybe the double quotes was somehow converting the
integer values to strings again but I got a SQL error instead so I don’t
think that is it either. Google and a search of the forums has not
shown me anything that is helping me figure out what is going on.
Anyone encountered something similar they can share some insights with?
Thanks.