【发布时间】:2018-06-10 09:19:15
【问题描述】:
我想对存储在 SQLite 数据库中的数字进行一些数学运算。我的查询似乎有效。我的问题是让 python 将我从数据库中查询的数字视为数字。
第一个定义从我的数据库中选择一个唯一的行。第二个定义循环遍历我的数据库中的许多行,并将它们添加到我想要求和的数字列表中。
当我尝试对数字求和时会出现问题。谁能指出我正确的方向?
这是我的一些代码:
def query_symbol(symbolId):
uniqueId = str(symbolId) + '@' + str(datetime.date.today())
conn = sqlite3.connect('sql/databse.db')
c = conn.cursor()
with conn:
c.execute("SELECT number FROM symbols WHERE uniqueId= ?", (uniqueId,))
return c.fetchall()
def calc(filename):
symbolIds = def_sec.load_symbolIds(filename)
print(len(symbolIds))
list = []
for symbolId in symbolIds:
data = query_symbol(symbolId)
#data = data.replace('()','')
list.append(data)
print(list)
total = sum(list)
print(total)
calc('index_symbolids')
我的错误信息如下所示:
65
[[(13506636000.0,)], [(20156784500.0,)], [(21361120000.0,)], [(4650564600.0,)], [(18572773200.0,)], [(13889340000.0,)], [(21911477100.0,)], [(19014765000.0,)], [(8592582800.0,)], [(12399850600.0,)], [(26021607500.0,)], [(17344514400.0,)], [(28396342200.0,)], [(10444843100.0,)], [(13894385900.0,)], [(26429184100.0,)], [(9193019800.0,)], [(18356516200.0,)], [(13693344800.0,)], [(39135783700.0,)], [(64988933000.0,)], [(52588381800.0,)], [(53514752300.0,)], [(8205312900.0,)], [(18563139800.0,)], [(34542681400.0,)], [(10626282600.0,)], [(14568874300.0,)], [(52083201800.0,)], [(21204153700.0,)], [(13380654000.0,)], [(24821311300.0,)], [(8232241800.0,)], [(148515191500.0,)], [(31669795700.0,)], [(97989223400.0,)], [(135145143799.0,)], [(178696200.0,)], [(9474728600.0,)], [(77661549000.0,)], [(33649778800.0,)], [(10061871500.0,)], [(23682872900.0,)], [(5196629500.0,)], [(54706667400.0,)], [(13934478600.0,)], [(5141383100.0,)], [(81343002200.0,)], [(16173162200.0,)], [(17649907400.0,)], [(32514907200.0,)], [(9783995600.0,)], [(75825589800.0,)], [(6205111500.0,)], [(53908007900.0,)], [(7615559400.0,)], [(17484345800.0,)], [(16072715900.0,)], [(53990182900.0,)], [(25798084100.0,)], [(28311485300.0,)], [(7296894200.0,)], [(19297000000.0,)], [(13271169800.0,)], [(22862203000.0,)]]
Traceback (most recent call last):
File "/Users/michael/atomProjects/calc.py", line 53, in <module>
index_calc('index_symbolids')
File "/Users/michael/atomProjects/calc.py", line 49, in calc
total = sum(list)
TypeError: unsupported operand type(s) for +: 'int' and 'list'
[Finished in 0.822s]
【问题讨论】: