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

控制台信息:

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  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值