dbutils module

class Utils.dbutils.Database(config_path, schema_name, echo=False)

Bases: object

Wrapper class for a postgres database accessed using SQLalchemy.

Parameters
  • config_path (str) – Path to access the database.

  • schema_name (str) – The default schema.

build_table(table_name, sql)

Create a table in the database, replacing it if it already exists.

Parameters
  • table_name (str) – The table to create

  • sql (str) – The query to be run to create the table

execute(*sqls)

Run each of the queries in sqls.

Parameters

sqls (list) – A list of strings, each of which is a SQL query to be run.

fast_query(sql)

Quickly get the results of a SQL query into a pandas.DataFrame by using an intermediate file buffer.

Parameters

sql (str) – The query to be run

Returns

Query result

Return type

(pd.DataFrame)

get_all_tables(schema='cdm')

Return a list of all tables in a schema.

Parameters

schema (str) – The schema to check for tables in.

Returns

A list of all tables in schema.

Return type

(list)

query(sql)

Get the results of a SQL query into a pandas.Dataframe.

Parameters

sql (str) – The query to be run

Returns

Query result

Return type

(pd.DataFrame)