In python SQLAlchemy offers many ways to create engines. pyodbc is one of them another is pymssql. I am looking for SQLAlchemy create engine code sample using MSSql server's service Principal id and service Principal password with acess token. If you have any kind of code similar to it please let me know

I have tried many combination of connection string to connect to engine so far I am. sucessful with pyodbc but my project wants me to use pymssql. the create engine works however it refuses to connect.

def _create_pool_sql_alchemy(self):
        method_name = '_create_pool_sql_alchemy'
        logging.info(f'entered into {method_name}')
        tokenb = bytes(self.access_token, "UTF-8")
        exptoken = b''
        for i in tokenb:
            exptoken += bytes({i})
            exptoken += bytes(1)
 
        tokenstruct = struct.pack("=i", len(exptoken)) + exptoken
        self.connString = self.driver + self.server + self.database
        SQL_COPT_SS_ACCESS_TOKEN = 1256
        params = urllib.parse.quote(self.connString)
        logging.info(f'entered into {method_name}, before engine create {params} , also tokenstruck {tokenstruct} ')
        self.alchemy_engine = sa.create_engine("mssql+pyodbc:///?odbc_connect={0}".format(params), connect_args={'attrs_before': {SQL_COPT_SS_ACCESS_TOKEN:tokenstruct}})
        logging.info(f'exit from {method_name}')
0

There are 0 best solutions below