先上代码:
require 'sqlite3'
class hx
sy=Array.new #收益
def cr_sp(pm,sl,md,cb,bz) #是插入商品资料
sql="INSERT INTO sp(pm,sl,md,cb,bz)VALUES('"+pm+"','"+sl+"','"+md+"','"+cb+"','"+bz+"')"
db=SQLite3::Database.new("C:\\Users\\sai\\Desktop\\kw\\kw.db")
db.execute(sql)
puts "完成!"
end
def cr_ss(sj,dh,pm,hszj,sl,yf_sf,yf_dd,kddh)
db=SQLite3::Database.new("C:\\Users\\sai\\Desktop\\kw\\kw.db")
sql1="INSERT INTO ss(sj,dh,pm,hszj,sl,yf_sf,yf_dd,kddh)VALUES('"+sj+"','"+dh+"','"+pm+"','"+hszj+"','"+sl+"','"+yf_sf+"','"+yf_dd+"','"+kddh+"')"
db.execute(sql1)
puts "完成!"
end
def sy_d(pm,dh)
db=SQLite3::Database.new("C:\\Users\\sai\\Desktop\\kw\\kw.db")
sql2="select hszj+yf_dd-yf_sf-(select cb from sp where pm='"+pm+"') from ss where dh='"+dh+"'"
sy=db.execute(sql2)
sy.each do |sy1|
puts "收益:"+sy1
@sy<<sy1
end
end
end
a=hx()
puts "1、插入新商品\n2、插入新订单\n3、计算收益"
xz=gets
if xz=="1"
puts "商品名:"
pm1=gets
puts "数量:"
sl1=gets
puts "目的:"
md=gets
puts "成本:"
cb=gets
puts "备注:"
bz=gets
a.cr_sp(pm1.chomp,sl1.chomp,md.chomp,cb.chomp,bz,chomp)
elsif xz=="2"
puts "时间:"
sj=gets
puts "订单号:"
dh=gets
puts "商品名:"
pm2=gets
puts "回收资金:"
hszj=gets
puts "数量:"
sl2=gets
puts "实付运费:"
yf_sf=gets
puts "订单运费:"
yf_dd=gets
puts "运单号:"
kddh=gets
a.cr_ss(sj.chomp,dh.chomp,pm2.chomp,hszj.chomp,sl2.chomp,yf_sf.chomp,yf_dd.chomp,kddh.chomp)
elsif xz=="3"
puts "起始时间:"
qs=gets
puts "结束时间:"
js=gets
sql3="select pm,dh from ss where sj<="+qs.chomp+"' and sj>='"+js.chomp+"'"
sy_d1=db.execute(sql1)
sy_d1.each do |p1,d1|
a.sy_d(p1,d1)
end
puts "时间段内收益:"
sum = 0
sy_p=sy.each { |a| sum+=a }
puts sy_p
else
puts "请做出你的选择!"
end
在运行中报错no implicit conversion of nil into String (TypeError),具体如下:
感觉很奇怪,请问是为什么?