Database

The database class facilitates interaction with external databases through SQLAlchemy, enabling users to execute SQL queries and retrieve results effortlessly. Upon initialization with a database connection string, it establishes a connection to the database.

The query method allows execution of SQL queries, returning a Query object that can fetch results in various formats such as dictionaries or Objectify objects. The Query nested class encapsulates the functionality for executing queries, fetching results, and offers methods to retrieve all results, the first or last result, or limit the number of results returned.

This class streamlines database operations, offering flexibility and ease of use for accessing and manipulating data from external databases.


# Example database connection string connstring = "postgresql://username:password@hostname:port/database" # Creating an instance of ExternalDatabase with the connection string external_db = database(connstring) # Example SQL query string query_string = "SELECT * FROM employees WHERE department='Finance'" # Executing the query and fetching all results as dictionaries results = external_db.query(query_string).all() # Printing the fetched results for result in results: logger.info(result) # Executing the query and fetching the first result as an Objectify object first_result = external_db.query(query_string).first(as_object=True) # Limiting the number of results to 5 and fetching them as dictionaries limited_results = external_db.query(query_string).limit(5)

 

 

Easy for Jira - Python Automations, 2023