Database

Description

class lab_utils.database.Database(config_file: Optional[str] = None, host: Optional[str] = None, port: Optional[int] = None, database: Optional[str] = None, user: Optional[str] = None, passfile: Optional[str] = None)[source]

Manages connections and operations with a PostgreSQL database. The class is based on the psycopg2 library and on this tutorial.

Attributes

Database.connection

Connection object returned by psycopg2.connect()

Database.cursor

Cursor provided by connection.cursor() to execute an SQL query

Database.database

The database name to connect to

Database.db_version

Database version

Database.host

The host name where the database is located

Database.passfile

Location of the pgpass file with the credentials

Database.port

Connection port

Database.user

User name

Methods

Database.__init__

Initializes the Database object.

Database.check_column

Checks if a column exists in a given table.

Database.check_empty_table

Checks whether a table is empty or not

Database.check_table

Checks if a table exists.

Database.close

Closes the connection to the database.

Database.config

Loads the configuration.

Database.connect

Connects to the database.

Database.create_aggregate_view

Creates a set of aggregate views in a given table.

Database.create_database

Creates a database named db_name.

Database.create_timescale_db

Creates a TimescaleDB table.

Database.fetch_latest_value

Retrieves the latest values of a time-ordered table.

Database.fetch_next_serial_id

Checks the latest ID of a serialized table and returns the next available one.

Database.get_list_columns

Fetches the list of columns in a given table.

Database.new_column

Creates a new column in a given table.

Database.new_entry

Inserts data into a given table.

Database.retrieve_entries

Retrieves entries from a table with given conditions.

Database.update_measurement

Updates the entry of a measurement.