diff --git a/pandasai/connectors/pandas.py b/pandasai/connectors/pandas.py index 109931b67..18f1e04ed 100644 --- a/pandasai/connectors/pandas.py +++ b/pandasai/connectors/pandas.py @@ -167,6 +167,13 @@ def enable_sql_query(self, table_name=None): table = table_name or self.name + # Check if the table already exists in DuckDB + existing_tables = duckdb.query("SHOW TABLES").fetchall() + + # If the table already exists, drop it + if table in [t[0] for t in existing_tables]: + duckdb.query(f"DROP TABLE {table}") + duckdb_relation = duckdb.from_df(self.pandas_df) duckdb_relation.create(table) self.sql_enabled = True