File size: 936 Bytes
b2add11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import duckdb
from dataclasses import dataclass


@dataclass
class User:
    id: int
    name: str
    email: str


def create_database():
    conn = duckdb.connect(database=":memory:")
    return conn


def create_table(conn):
    conn.execute("CREATE TABLE users (id INTEGER, name VARCHAR, email VARCHAR)")


def insert_data(conn, users):
    conn.execute("INSERT INTO users (id, name, email) VALUES (?, ?, ?)", users)


def query_data(conn):
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM users")
    return cursor.fetchall()


def main():
    conn = create_database()
    create_table(conn)

    users = [
        (1, "John Doe", "john@example.com"),
        (2, "Jane Doe", "jane@example.com"),
        (3, "Bob Smith", "bob@example.com"),
    ]

    for user in users:
        insert_data(conn, user)

    results = query_data(conn)
    for row in results:
        print(row)


if __name__ == "__main__":
    main()