控制台信息:
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"
- require 'rubygems'
- require_gem 'activerecord' # 请注意使用rubygems时候的声明方式
- # 连接数据库,按需求填写各种参数。
- ActiveRecord::Base.establish_connection(
- :adapter => "postgresql",
- :host => "localhost",
- :username => "admin",
- :password => "admin",
- :database => "hello")
- class Resource < ActiveRecord::Base # 开始ORM对应
- set_table_name 'resource' # 指定数据库名
- end
- # 插入一条数据
- tab = Resource.new("id" => 8)
- tab.id = 8
- tab.name = 'ActiveRecord'
- tab.save