无法对浮点迭代求和

问题描述 投票:0回答:1

qazxsw poi(sqlite3与更新的ruby兼容性问题-v)qazxsw poi ruby -v 2.3.7 sqlite3 -v 3.22

我尽可能地浓缩了代码。

sqlite -v 2.8

我主要使用lsb_release -a 18.04方法。我依赖于def some_method(info_hash) ... db.results_as_hash = true sum = 0 db.transaction db.execute2 "CREATE table if not exists a_table(Id INT PRIMARY KEY, Type TEXT, Month TEXT, Amount FLOAT, TOTAL FLOAT)" db.execute2 "INSERT into a_table(Type, Month, Amount) values(:Type , :Month , :Amount)", info_hash[:category], info_hash[:month], info_hash[:amount] get_amt = db.prepare "SELECT Amount from a_table WHERE Type = :Type" get_amt.execute info_hash[:category] get_amt.each do |g| sum += g #here I get a NoMethodError for Nil:NilClass end db.execute2 "INSERT into bills(Total) values(:sum)", sum db.commit ... end 方法,我需要忽略标题,例如在我的execute2块中。

我想executeget_amt.eachsum专栏。但是当我运行我的Amount时,我遇到了Type

完整的错误是:NoMethodError

请告知我哪里出错了。

编辑:我重写了代码以反映@Shawn的建议:

block

这产生了undefined method '+' for nil:NilClass (NoMethodError) def some_method(info_hash) ... db.transaction db.execute2 "CREATE table if not exists a_table(Id INTEGER PRIMARY KEY, Type TEXT, Month TEXT, Amount FLOAT, Total FLOAT)" db.execute2 "INSERT into a_table(Type, Month, Amount) values(:Type , :Month , :Amount)", info_hash[:category], info_hash[:month], info_hash[:amount] get_amt = db.execute2 "SELECT sum(Amount) from a_table WHERE Type = :Type", info_hash[:category] db.execute2 "INSERT into a_table(Total) values(:get_amt)", get_amt db.commit ... end

ruby sqlite each nomethoderror
1个回答
0
投票
sqlite3 Exception

本质上,语句Index out of range将超过1的值写入db.execute2 "INSERT into a_table(Total) values(:get_amt)", get_amt[1][0] 变量。该变量必须与get_amt = db.execute2 "SELECT sum(Amount) from a_table WHERE type = :type", hash_info[category]使用的具体值的get_amtrow一起提供。因此column或第一行(标题行后的下一行)和第一列。

© www.soinside.com 2019 - 2024. All rights reserved.