def build_format_file(df: pd.DataFrame, delimiter: str, db_cols_order: Optional[Dict[(str, int)]]=None) -> str:
    "\n    Creates the non-xml SQL format file. Puts 4 spaces between each section.\n    See https://docs.microsoft.com/en-us/sql/relational-databases/import-export/non-xml-format-files-sql-server\n    for the specification of the file.\n\n    # TODO add params/options to control:\n    #   - the char type (not just SQLCHAR),\n\n    Parameters\n    ----------\n    df : pandas DataFrame\n    delimiter : a valid delimiter character\n    db_cols_order : dict, optional\n        Dict of {database column name -> ordinal position of the column}.\n        Maps existing columns in the database to their ordinal position, i.e. the order of the columns in the db table.\n        1-indexed, so the first columns is 1, second is 2, etc.\n        Only needed if the order of the columns in the dataframe doesn't match the database.\n\n    Returns\n    -------\n    A string containing the format file\n    "
    _space = (' ' * 4)
    format_file_str = f'''9.0
{len(df.columns)}
'''
    for (col_num, col_name) in enumerate(df.columns, start=1):
        _delim = (delimiter if (col_num != len(df.columns)) else NEWLINE)
        _line = _space.join([str(col_num), SQLCHAR, str(0), str(0), f'"{_escape(_delim)}"', str((col_num if (not db_cols_order) else db_cols_order[str(col_name)])), str(col_name), sql_collation, '\n'])
        format_file_str += _line
    return format_file_str
Creates the non-xml SQL format file. Puts 4 spaces between each section.
See https://docs.microsoft.com/en-us/sql/relational-databases/import-export/non-xml-format-files-sql-server
for the specification of the file.

# TODO add params/options to control:
#   - the char type (not just SQLCHAR),

Parameters
----------
df : pandas DataFrame
delimiter : a valid delimiter character
db_cols_order : dict, optional
    Dict of {database column name -> ordinal position of the column}.
    Maps existing columns in the database to their ordinal position, i.e. the order of the columns in the db table.
    1-indexed, so the first columns is 1, second is 2, etc.
    Only needed if the order of the columns in the dataframe doesn't match the database.

Returns
-------
A string containing the format file
def build_format_file(df: pd.DataFrame, delimiter: str, db_cols_order: Optional[Dict[(str, int)]]=None) -> str:
    "\n    Creates the non-xml SQL format file. Puts 4 spaces between each section.\n    See https://docs.microsoft.com/en-us/sql/relational-databases/import-export/non-xml-format-files-sql-server\n    for the specification of the file.\n\n    # TODO add params/options to control:\n    #   - the char type (not just SQLCHAR),\n\n    Parameters\n    ----------\n    df : pandas DataFrame\n    delimiter : a valid delimiter character\n    db_cols_order : dict, optional\n        Dict of {database column name -> ordinal position of the column}.\n        Maps existing columns in the database to their ordinal position, i.e. the order of the columns in the db table.\n        1-indexed, so the first columns is 1, second is 2, etc.\n        Only needed if the order of the columns in the dataframe doesn't match the database.\n\n    Returns\n    -------\n    A string containing the format file\n    "
    _space = (' ' * 4)
    format_file_str = f'9.0
{len(df.columns)}
'
    for (col_num, col_name) in enumerate(df.columns, start=1):
        _delim = (delimiter if (col_num != len(df.columns)) else NEWLINE)
        _line = _space.join([str(col_num), SQLCHAR, str(0), str(0), f'"{_escape(_delim)}"', str((col_num if (not db_cols_order) else db_cols_order[str(col_name)])), str(col_name), sql_collation, '\n'])
        format_file_str += _line
    return format_file_str