October 23, 2024
Chicago 12, Melborne City, USA
python

Querying class members variables with DuckDB


I have a codebase with an API to pass a SQL query to run for execution, but that same API does not provide a way to pass a variable. Meaning I do not get to pass a variable in the same scope as where the query runs:

class SampleClientClass(BaseExecutionAPI):
    def __init__(self):
        self.conn = duckdb.DuckDBPyConnection()
    
    def _execute_queries(self):
       qs = self._yield_queries()
       for q in qs:
          self.conn.sql(q)

    def _yield_queries(self):
       ...

I have tried passing it as a class member instead (since I do not get to override or alter the _execute_queries method) but that does not seem to work:

class MyTableCreationClass(BaseExecutionAPI):
    def __init__(self, df: pd.DataFrame):
        self.conn = duckdb.DuckDBPyConnection()
        self.my_df = df
    
    def _execute_queries(self):
       qs = self._yield_queries()
       for q in qs:
          self.conn.sql(q)

    def _yield_queries(self):
        return [
        f"""
        INSERT INTO my_table BY NAME
        SELECT * FROM \"self.my_df\"
        """
        ]

Which gives me the error:
Table with name self.my_df does not exist!

Is there a way to have DuckDB pick up class members at all, or does it only work with local variables?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video