#encoding=UTF-8 require "mysql" dbh=Mysql.real_connect("localhost","root","root","rubytest",) class User def initialize(id,name,pwd) #构造方法 @id=id @name=name @pwd=pwd end attr_accessor :id,:name,:pwd #修饰可读、可写属性 end class UserDao #登录操作 def login(user,dbh) sql="select * from ruby where name ='#{user.name}' and pwd='#{user.id}'" res=dbh.query(sql) if res.num_rows==0 then puts "login fail" else puts "login success" end end #注册操作 def register(user,dbh) sql="insert into ruby values('#{user.id}','#{user.name}','#{user.pwd}')" dbh.query(sql) if dbh.affected_rows==i then puts "register success" else pus "register fail" end #按条件查询 def findAll(dbh,name) sql="select * from ruby where name='#{name}'" res=dbh.query(sql) puts "ID\tNAME\tPWD" while item=res.fetch_row puts "#{item[0]}\t#{item[1]}\t#{item[2]}" end end end begin user=User.new(8,'bf001','001') #登录用 user=User.new(9,'zs','123') #创建 User 对象 ud=UserDao.new #创建 UserDao 对象 #ud.login(user,dbh) 登录操作 #注册用 ud.register(user,dbh) ud.findALL(dbh,'bf001') #查询 end end
报错:test.rb:49:in <class:UserDao>': undefined local variable or method
dbh' for UserDao:Class (NameError)
from test.rb:13:in `