File size: 915 Bytes
3d2aa58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import duckdb

def insert(full_response, message):
    age = 28
    db_path = "./workspace/sample.duckdb"
    con = duckdb.connect(database=db_path)
    con.execute(
        """
        CREATE SEQUENCE IF NOT EXISTS sample_id_seq START 1;
        CREATE TABLE IF NOT EXISTS samples (
            id INTEGER DEFAULT nextval('sample_id_seq'),
            name VARCHAR,
            age INTEGER,
            PRIMARY KEY(id)
        );
        """
    )
    cur = con.cursor()
    con.execute("INSERT INTO samples (name, age) VALUES (?, ?)", (full_response, age))
    con.execute("INSERT INTO samples (name, age) VALUES (?, ?)", (message, age))
    con.execute("COPY samples TO 'sample.csv' (FORMAT CSV, HEADER)")
    con.commit()
    cur = con.execute("SELECT * FROM samples")
    res = cur.fetchall()
    rows = ""
    rows = "\n".join([f"name: {row[0]}, age: {row[1]}" for row in res])
    con.close()
    return rows