1 | n/a | import sqlite3 |
---|
2 | n/a | |
---|
3 | n/a | class MySum: |
---|
4 | n/a | def __init__(self): |
---|
5 | n/a | self.count = 0 |
---|
6 | n/a | |
---|
7 | n/a | def step(self, value): |
---|
8 | n/a | self.count += value |
---|
9 | n/a | |
---|
10 | n/a | def finalize(self): |
---|
11 | n/a | return self.count |
---|
12 | n/a | |
---|
13 | n/a | con = sqlite3.connect(":memory:") |
---|
14 | n/a | con.create_aggregate("mysum", 1, MySum) |
---|
15 | n/a | cur = con.cursor() |
---|
16 | n/a | cur.execute("create table test(i)") |
---|
17 | n/a | cur.execute("insert into test(i) values (1)") |
---|
18 | n/a | cur.execute("insert into test(i) values (2)") |
---|
19 | n/a | cur.execute("select mysum(i) from test") |
---|
20 | n/a | print(cur.fetchone()[0]) |
---|