今天在使用使用ruby-dbi时出现了一些问题。首先装dbi得要三条命令,刚开始少了一条,我说怎么老说错误的哈,命令,这里我装的是mysql的驱动,其他类似:
[code]
ruby setup.rb config --with=dbi,dbd_mysql
ruby setup.rb setup
ruby setup.rb install[/code]
然后我在运行这样一个程序时出现了in `+': can't convert Fixnum into String (TypeError)的错误:
[code]
require 'dbi'
begin
dbh=DBI.connect("dbi:Mysql:test1:localhost","root","password")
sth=dbh.execute("select count(id) from test1")
puts "BookCount"+sth.fetch[0]#这里出现的错误哈
sth.finish
begin
sth=dbh.prepare('select * from test1')
sth.execute
while row=sth.fetch do
p row
end
sth.finish
rescue
end
end[/code]
而java里是不会出现这种情况的,这里我们只需要把sth.fetch[0]改成sth.fetch[0].to_s就可以正确运行了。
[code]
ruby setup.rb config --with=dbi,dbd_mysql
ruby setup.rb setup
ruby setup.rb install[/code]
然后我在运行这样一个程序时出现了in `+': can't convert Fixnum into String (TypeError)的错误:
[code]
require 'dbi'
begin
dbh=DBI.connect("dbi:Mysql:test1:localhost","root","password")
sth=dbh.execute("select count(id) from test1")
puts "BookCount"+sth.fetch[0]#这里出现的错误哈
sth.finish
begin
sth=dbh.prepare('select * from test1')
sth.execute
while row=sth.fetch do
p row
end
sth.finish
rescue
end
end[/code]
而java里是不会出现这种情况的,这里我们只需要把sth.fetch[0]改成sth.fetch[0].to_s就可以正确运行了。