| import sqlite3, os | |
| def init_db(db_path="data/results.db"): | |
| os.makedirs(os.path.dirname(db_path), exist_ok=True) | |
| conn = sqlite3.connect(db_path) | |
| conn.execute(""" | |
| CREATE TABLE IF NOT EXISTS exprs( | |
| expr TEXT PRIMARY KEY, | |
| score REAL, | |
| rationale TEXT, | |
| complexity TEXT, | |
| created_at TEXT | |
| )""") | |
| conn.commit() | |
| return conn | |
| def add_expr(conn, expr, score, rationale="", complexity=""): | |
| conn.execute("INSERT OR REPLACE INTO exprs(expr, score, rationale, complexity, created_at) VALUES(?,?,?,?,datetime('now'))", | |
| (expr, score, rationale, complexity)) | |
| conn.commit() | |
| def top_exprs(conn, k=5, min_score=0.0): | |
| cur = conn.cursor() | |
| cur.execute("SELECT expr,score,rationale FROM exprs WHERE score>=? ORDER BY score DESC LIMIT ?", (min_score,k)) | |
| return cur.fetchall() | |