def create_db(root_username, root_password, base_name, db_username, db_password):

    # Connect to MySQL.
    engine = connect(root_username, root_password)

    # Find a suitable name:
    # Get a list of all current databases.
    dbs = engine.execute(text("SHOW databases LIKE :bn"), bn="%s%%" % base_name)
    dbs = dbs.fetchall()
    dbs = [db[0] for db in dbs]
    # Find the first non-existing name which satisfies the criteria.
    db_name = None
    for i in range(1, 10000):
        db_name = "%s%d" % (base_name, i)
        if db_name not in dbs:
            break

    if db_name is None:
        raise Exception("No suitable db_name found")

    # Create the database.
    engine.execute("CREATE DATABASE %s DEFAULT CHARACTER SET utf8" % db_name)

    # It is not necessary to grant privileges. We assume that the deploy script
    # was run, which grants privileges to wcloud_creator and wcloud on 'wcloud%'
    # databases.
    # engine.execute("GRANT ALL PRIVILEGES ON %s.* TO %s@localhost" % (db_name, db_username))

    # Test that the database was created successfully.
    engine = connect(db_username, db_password)
    engine.execute("USE %s" % db_name)
    engine.execute("SHOW tables").fetchall()

    # If no exception was thrown it was done right.

    return db_name
Task to create a MySQL database upon request, which will be used by a WCLOUD instance.
dataset/ETHPy150Open weblabdeusto/weblabdeusto/tools/wcloud/wcloud/actions/db_actions.py/create_db