Ruby连Mysql的时候,按照书上出不来结果。
上网查了下,主要有拷贝libmysql.dll到Ruby内目录bin下,安装路径不能为空,别人通过这2种方法可以连通Mysql,可到我这边就不行,后来查到要拷贝mysql.so后才终于连接成功。
最后总结下,基本步骤如下:
1,装Ruby和Rails,若Rails在线安装不行的话,就从http://rubyforge.org/frs/?group_id=307去下载
rails-2.2.2.gem(附件里也有2.2.2的),例:gem install rails-2.2.2.gem 就行了。
2,装Mysql(路径中不能有空格)并去http://rubyforge.org/projects/mysql-win把Mysql的补丁包打上,例:gem install mysql-2.7.3-mswin32.gem
3,把Mysql安装目录bin下的libmysql.dll拷到Ruby安装目录bin下
4,最后也是最重要的,把
mysql.so(附件里只支持mysql5的版本)文件拷贝到ruby安装目录下的\lib\ruby\site_ruby\1.8\i386-msvcrt中。
接下来用测试连接代码试一下
require 'mysql' #包含MySQL接口
begin
# 尝试连接MySQL数据库
my = Mysql.connect('localhost', 'root', 'root', 'mytest')
# 如果成功,显示MySQL数据库版本
puts "服务器版本: " + my.get_server_info
rescue Mysql::Error => err
# 如果失败,显示错误信息
puts "错误代码: #{err.errno}"
puts "错误信息: #{err.error}"
ensure
# disconnect from server
my.close if my
end
分享到:
相关推荐
FXRuby.Create.Lean.and.Mean.GUIs.with.Ruby
Design.Patterns.in.Ruby
why.s.poignant.guide.to.ruby.pdf
Refactoring.in.Ruby.pdf
Pragmatic.Programming.Ruby.1.9.Apr.2009
Packt.Cloning.Internet.Applications.with.Ruby
Pragmatic.Metaprogramming.Ruby.Feb.2010.rar
Addison.Wesley.Distributed.Programming.with.Ruby.Nov.2009.rar
No.Starch.The.Book.of.Ruby.Jun.2011.rar
ruby 连接mysql的gem安装包
Packt.Publishing.Building.Dynamic.Web.2.0.Websites.with.Ruby.on.Rails.Apr.2008.pdf
Build.Awesome.Command-Line.Applications.in.Ruby
manning.ruby.in.practice.mar.2009.pdf
Pragmatic.Bookshelf.Programming.Ruby.1.9.Apr.2009
No.Starch.The.Book.of.Ruby
Apress.Practical.Ruby.Gems.Apr.2007.pdf is about ruby and gems
No.Starch.Press.Ruby.by.Example.Concepts.and.Code