【发布时间】:2014-05-06 04:25:57
【问题描述】:
所以我有一个运行良好的 Ruby 程序来创建我的所有表,另一个是用于修改数据库的基本用户界面。我猜我不能只选择事物并将其分配给变量吗?我不确定我们到底错在哪里。此代码从不执行菜单,它在询问密码后停止。任何建议都会很棒,我在 Ruby 数据库在线资料中找到的大部分内容都与 Rails 相关。
#!/usr/bin/ruby
require 'sqlite3'
begin
db = SQLite3::Database.open "database.db"
puts "Enter Employee ID:"
empID = gets.chomp
puts "Enter Password:"
passInput = gets.chomp
empPass = db.execute "SELECT password FROM employees WHERE employeeID = #{empID}"
if empPass == passInput
menu
end
def menu
i = 0
loop do
puts "1. Find a dvd.","2. Display all current inventory.","3. Enter a sale.","4. Add a DVD to inventory.",
"5. Update inventory.","6. Quit."
input = gets.chomp
case input
when "1"
puts "Enter sku"
skuInput = gets.chomp
db.execute "SELECT * FROM dvds WHERE sku = #{skuInput}"
when "2"
db.execute "SELECT * FROM dvds WHERE numInInventory > 0"
when "3"
puts "Enter/Scan DVD Sku."
saleSku = gets.chomp
db.execute "UPDATE dvds SET numInInventory = numInInventory - 1 WHERE sku = #{salesSku}"
when "4"
puts "Enter Sku."
skuInsert = gets.chomp
puts "Enter DVD title."
titleInsert = gets.chomp
puts "Enter release year."
yearInsert = gets.chomp
puts "Enter length of film in minutes."
lengthInsert = gets.chomp
puts "Enter price."
priceInsert=gets.chomp
isThereInventory = db.execute "SELECT sku FROM dvds WHERE sku = #{skuInsert}"
db.execute "INSERT INTO dvds VALUES(#{skuInsert}, #{titleInsert}, #{yearInsert}, 1, #{timeInsert}, #{priceInsert})"
when "5"
puts "Enter sku of the DVD you wish to update."
updateSku = gets.chomp
puts "Enter the number of copies you are adding to inventory."
updateQuantity = gets.chomp
db.execute "UPDATE dvds SET numInInventory = numInInventory + #{updateQuantity} WHERE sku = #{updateSku}"
when "6"
puts "Goodbye!"
i = 1
else
puts "Invalid option: #{input}"
end
break if i == 1
end
end
rescue SQLite3::Exception => e
puts "Exception occured"
puts e
ensure
db.close if db
end
【问题讨论】:
-
passInput = gets.chomp链接之后的 anything 是否运行?如果您只是在捕获密码后添加一个简单的puts 'Hello, world!'行并完全忽略数据库问题,它是否有效?换句话说,您确定是数据库而不是以某种方式捕获密码的行吗? -
或者,您是否需要在 select 语句的末尾使用分号?
-
是的,如果我将 Hello World 放在 passInput = gets.chomp 之后,那么它确实有效。我猜它没有正确检查数据库的密码,所以 if 语句没有激活?
-
如果在查询后面加上
puts empPass,会得到什么? -
@UriAgassi program.rb:15:in
<main>': undefined local variable or methodempPass' for main:Object (NameError)