`
dawnzhang
  • 浏览: 43561 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Ruby学习--用ActiveRecord操作数据库错误之连接1

    博客分类:
  • Ruby
阅读更多

控制台信息:

c:/ruby/lib/ruby/1.8/readbytes.rb:21:in `read': Invalid argument (Errno::EINVAL)
 from c:/ruby/lib/ruby/1.8/readbytes.rb:21:in `readbytes'
 from c:/ruby/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/postgres-pr/message.rb:32:in `read'
 from c:/ruby/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/postgres-pr/connection.rb:30:in `initialize'
 from c:/ruby/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/postgres-pr/connection.rb:29:in `loop'
 from c:/ruby/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/postgres-pr/connection.rb:29:in `initialize'
 from c:/ruby/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/postgres-pr/postgres-compat.rb:23:in `new'
 from c:/ruby/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/postgres-pr/postgres-compat.rb:23:in `initialize'
 from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/connection_adapters/postgresql_adapter.rb:24:in `connect'
  ... 9 levels...
 from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/base.rb:1490:in `initialize_without_callbacks'
 from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/callbacks.rb:225:in `initialize'
 from test.rb:17:in `new'
 from test.rb:17

看完有点莫名其妙,但可以肯定的是:错误来自数据库。找到PostgreSQl的日志:

2007-02-11 23:13:38 FATAL:  database "hello" does not exist
2007-02-11 23:18:48 FATAL:  database "hello" does not exist

原来如此,数据库名称不确,用SQLServer用惯了,难免出现这种大小写错误,立马改过"Hello"

ruby 代码
  1. require 'rubygems'   
  2. require_gem 'activerecord'  # 请注意使用rubygems时候的声明方式   
  3.   
  4. # 连接数据库,按需求填写各种参数。   
  5. ActiveRecord::Base.establish_connection(   
  6. :adapter => "postgresql",   
  7. :host => "localhost",    
  8. :username => "admin",   
  9. :password => "admin",   
  10. :database => "hello")   
  11.   
  12. class Resource < ActiveRecord::Base # 开始ORM对应   
  13.   set_table_name 'resource' # 指定数据库名   
  14. end  
  15.   
  16. # 插入一条数据   
  17. tab = Resource.new("id" => 8)   
  18. tab.id = 8   
  19. tab.name = 'ActiveRecord'   
  20. tab.save  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics