sqlite3自定义聚合函数
import sqlite3 class custom_concat: def __init__(self): self.retval=[] def step(self,value): self.retval.append(value) def finalize(self): return ','.join(self.retval) # 连接 SQLite 数据库 conn = sqlite3.connect(':memory:') conn.create_aggregate("custom_concat", 1, custom_concat) # 示例查询 conn.execute(''' CREATE TABLE users ( type TEXT, v TEXT );''') for i in range(3): conn.execute(f'''INSERT INTO users (type,v) VALUES ("a",{i})''') for i in range(3): conn.execute(f'''INSERT INTO users (type,v) VALUES ("b",{i})''') print(conn.execute(''' SELECT type,custom_concat(v) FROM users group by type;''').fetchall()) #[('a', '0,1,2'), ('b', '0,1,2')]