File size: 1,104 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
import duckdb


class DuckDBManager:
    def __init__(self, db_path):
        self.db_path = db_path
        self.conn = duckdb.connect(database=self.db_path)

    def create_table(self, table_name, columns):
        query = f"CREATE TABLE IF NOT EXISTS {table_name} ({', '.join(columns)})"
        self.conn.execute(query)

    def insert(self, table_name, values):
        query = f"INSERT INTO {table_name} VALUES ({', '.join(['?' for _ in values])})"
        self.conn.execute(query, values)

    def select(self, table_name, columns):
        query = f"SELECT {', '.join(columns)} FROM {table_name}"
        return self.conn.execute(query).fetchall()

    def update(self, table_name, set_columns, where_clause):
        query = f"UPDATE {table_name} SET {', '.join([f'{col} = ?' for col in set_columns])} WHERE {where_clause}"
        self.conn.execute(query, [set_columns[col] for col in set_columns])

    def delete(self, table_name, where_clause):
        query = f"DELETE FROM {table_name} WHERE {where_clause}"
        self.conn.execute(query)

    def close(self):
        self.conn.close()