Plese follow the below steps in UiPath & python code as well
- In UiPath, create a Python script activity and pass the orchestrator DB asset as an input parameter to the Python script.
- In the Python script, retrieve the input parameter and store it in a variable.
- Use the
decrypt
function from thecryptography
library to decrypt the DB password. - Use a Python library such as
pyodbc
to connect to the DB using the decrypted password and other necessary connection details.
Sample Code:
import pyodbc
from cryptography.fernet import Fernet
def decrypt(key, ciphertext):
f = Fernet(key)
return f.decrypt(ciphertext)
def main(db_asset):
# Retrieve the password from the DB asset and decrypt it
password = decrypt(db_asset[‘Key’], db_asset[‘Password’])
# Connect to the DB using pyodbc
connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};'
'SERVER=' + db_asset['Server'] + ';'
'DATABASE=' + db_asset['Database'] + ';'
'UID=' + db_asset['Username'] + ';'
'PWD=' + password)
# Do something with the connection
cursor = connection.cursor()
cursor.execute("SELECT * FROM my_table")
rows = cursor.fetchall()
for row in rows:
print(row)
Regards,
Kaviyarasu N