Ich möchte Mathe mit Zahlen, die ich in einer SQLite-Datenbank gespeichert habe. Meine Abfrage scheint zu funktionieren. Mein Problem ist Python behandeln die Zahlen, die ich aus der Datenbank als Zahlen abgefragt habe.Summieren von Zahlen aus einer Python SQLite3-Abfrage
Die erste Definition wählt eine eindeutige Zeile aus meiner Datenbank aus. Die zweite Definition durchläuft eine Reihe von Zeilen in meiner Datenbank und fügt sie einer Liste von Zahlen hinzu, die ich zusammenfassen möchte.
Das Problem tritt auf, wenn ich versuche, die Zahlen zu summieren. Kann mir jemand in die richtige Richtung zeigen?
Hier einige meiner Code:
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')
Meine Fehlermeldung wie folgt aussieht:
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]
Vielen Dank. Würde es Ihnen etwas ausmachen, mich zu einer Dokumentation zu führen, die erklärt, wie "(data,)" mein Problem behebt? Wirklich große Hilfe. Danke noch einmal. –