Database Existence Check and Creation

  • Share this:

Code introduction


This function checks if the specified database exists and creates it if it does not.


Technology Stack : SQLAlchemy-Utils

Code Type : Function

Code Difficulty : Intermediate


                
                    
from sqlalchemy_utils import database_exists, create_database

def create_database_if_not_exists(engine, db_name):
    """
    This function checks if the database exists and creates it if it does not.

    :param engine: SQLAlchemy engine to use for database operations.
    :param db_name: Name of the database to check and create.
    """
    if not database_exists(engine, db_name):
        create_database(engine, db_name)