Ejemplo n.º 1
0
    def __to_db(self,
                dataframe: DataFrame,
                conn: Engine,
                params,
                **kwargs) -> str:
        table_name = params.get("table_name")
        batch_size = params.get("batch_size")
        mode = params.get("mode", 'append')
        index_flag = params.get("index")
        index_label = params.get("index_label")

        try:
            if mode == 'truncate' and conn.has_table(table_name=table_name):
                conn.execution_options(autoCommit=True)\
                    .execute(f"""TRUNCATE TABLE {table_name}""")
            dataframe.to_sql(con=conn,
                             name=table_name,
                             if_exists=self.modes.get(mode),
                             chunksize=batch_size,
                             index=index_flag,
                             index_label=index_label,
                             **kwargs)
        except Exception as err:
            msg = ("Error: Check your credentials (username,"
                   " password, host, port, database)\n")
            raise ValueError(msg, err)
Ejemplo n.º 2
0
 def __init__(self, engine: Engine):
     self.engine = engine.execution_options()
     event.listen(self.engine,
                  "before_cursor_execute",
                  self.on_before_execute,
                  retval=True)
     event.listen(self.engine, "after_cursor_execute",
                  self.on_after_execute)
     event.listen(self.engine, "handle_error", self.on_error)