ZaToxin
ZaToxin

Reputation: 1

SQLite python - Convert value into variable

I have a table that has a column with different numbers in it. So it looks like

Name Number
John 22
Max 28
Debbie 22
John 21
John 52

How can I add up the Number column and return it to a variable

What I have now, will the number for each row matching the name, but it returns it like this: [(22),(21),(52)]

Ideally, I'd like to be able to search with select, and return total into an int variable.

  def get_num():
            with conn:
                c.execute("SELECT Number FROM table WHERE name=John")
                x = (c.fetchall())

                return x

        print(get_num())

Upvotes: 0

Views: 71

Answers (1)

John Gordon
John Gordon

Reputation: 33335

Use the SQL SUM function.

c.execute("SELECT SUM(Number) FROM table WHERE name='John'")
return c.fetchone()[0]

Upvotes: 2

Related Questions