我从我的数据库中检索了一个Float值,并尝试将其添加到Float类型值,我收到错误:in '+': Array cant be coerced into Float (TypeError)
total = 0.0
db = SQLite3::Database.open "Checkout.sqlite"
for i in 0..@items.length
tempPrice = db.execute "SELECT price FROM Products WHERE product_code = ?", @items[i]
total = total + tempPrice
end
答案 0 :(得分:1)
tempPrice
是数组,如果您确定它只有1个值,则应使用tempPrice[0]
;如果结果可以有多个值,则可以使用tempPrice.sum
。
UPD: 尝试
total = 0.0
db = SQLite3::Database.open "Checkout.sqlite"
@items.each do |item|
db.execute("SELECT price FROM Products WHERE product_code = ?", item) do |row|
total = total + row[0]
end
end
答案 1 :(得分:0)
total = 0.0
db = SQLite3::Database.open "Checkout.sqlite"
for i in 0..@items.length
db.execute("SELECT price FROM Products WHERE product_code = ?", @items[i]) do |row|
total = total + row[0]
end
end