class Score
def initialize(id=0,subject,socre,student_name,explain)
@id =id
@subject =subject
@score =score
@student_name =student_name
@explain=explain
end
attr_accessor :id,:subject,:score,:student_name,:explain
end
require "mysql"
def createSQL(score)
"insert into scores(subject,score,student_name,`explain`)"+
"values('#{score.subject}',#{score.score},'#{score.student_name}','#{score.explain}')"
end
def saveScore(dbh,score)
sql=createSQL(score)
dbh.query(sql)
if(dbh.affected_rows==1)
puts "学生 #{score.student_name} 的成绩已经被放入数据库"
end
end
peter=Score.new("数学",83,"Peter","")#提示的位置
libei=Score.new("数学",100,"李贝","")
liubuyi=Score.new("数学",58,"刘不一","因为迟到,题没做完")
begin
dbh=Mysql.real_connect("localhost","root","root","db_student")
saveScore(dbh,peter)
#saveScore(dbh,libei)
#saveScore(dbh,liubuyi)
#
#sql="update scores set score =#{86} where student_name='#{"peter"}'"
#dbh.query(sql)
#if(dbh.affected_rows==1)
# puts "学生#{"李贝"}的成绩删除成功"
#end
rescue Mysql::Error=>e
puts "Error Number: #{e.errno}"
puts "Error Number: #{e.errno}"
puts "Error Message: #{e.error}"
puts "Error State: #{e.sqlstate}" if e.respond_to?("sqlstate")
ensure
dbh.close
end这是运行ruby文件后的提示求解如何修改
添加回答
举报
0/150
提交
取消