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