dautil.db

Database related utilities.

dautil.db.count_where(session, column, search_val)
Gets the count of a query as in the following SQL SELECT count(column) from atable where atable = ‘search_val’

Note

DO NOT USE when high-performance is required.

Parameters:
  • session – A SQLAlchemy session.
  • column – A table column as attribute in a Python class.
  • search_val – Value to search for.
Returns:

The count of the query.

dautil.db.create_session(dbname, base, prefix='sqlite:///{}')

Creates a database session.

Parameters:
  • dbname – The name of the database.
  • base – The declarative Base class.
  • prefix – The string at the start of the database URL.
Returns:

The database session.

dautil.db.entity_from_column(column)

Utility function which returns the entity of a column.

Parameters:column – A table column as attribute in a Python class.
Returns:The entity.
dautil.db.map_to_id(session, column)

Assuming that a table has a primary key column ‘id’. This function creates dictionary with column value as key and id as value.

Parameters:
  • session – A SQLAlchemy session.
  • column – A table column as attribute in a Python class.
Returns:

A dictionary with column values mapped to id.

dautil.db.not_empty(session, table)

Checks whether a table is empty.

Parameters:
  • session – A SQLAlchemy session.
  • table – The entity class of a database table.
Returns:

True if the table is not empty.

dautil.db.where_first(session, column, search_val)

Gets the first row as in the following SQL SELECT * from atable where atable = ‘search_val’

Parameters:
  • session – A SQLAlchemy session.
  • column – A table column as attribute in a Python class.
  • search_val – Value to search for.
Returns:

The first row of the query.

Previous topic

dautil.data

Next topic

dautil.log_api

This Page