0

I've built a Python script that runs a report for me using one of our vendor's API's, then uploads the data directly to an MS SQL server. I would like to add an error handler that sends an email when the insert fails for any reason.

Can I just wrap my insert statement in a try? Currently I have this going to a local server for testing...

 conn = pyodbc.connect('Driver={SQL Server};'
                  'Server=localhost\*****local;'
                  'Database=Reporting;'
                  'Trusted_Connection=yes;')

 #Set cursor variable
 cursor = conn.cursor()

 executeValue = """INSERT INTO New_Five9_CallLog 
    (Call_ID, [Timestamp],
    Campaign, Call_Type, Agent_Email, Agent_Name, Disposition,
    ANI, Customer_Name, DNIS, Call_Time, Rounded_Bill_Time,
    Cost, IVR_Time, Queue_Wait_Time, QCB_Wait_Time,
    Total_Queue_Time, Ring_Time, Talk_Time, Hold_Time, Park_Time,
    ACW_Time, Transfers, Conferences, Holds, Parks, Abandoned,
    Recordings, Handle_Time, Session_ID, IVR_Path,
    Skill, Ticket_Number)
    VALUES (""" + values + ")"

#Execute query
cursor.execute(executeValue)

#Commit and close
conn.commit()
conn.close()

I get the values variable with some other script above this section. What I'd like to know is how to capture an error on this section and then send an email to myself with the error description.

a121
  • 798
  • 4
  • 9
  • 20
David Cady
  • 61
  • 6

1 Answers1

0

Check out this answer https://stackoverflow.com/a/42143703/1525867 explaining how to catch pyodbc specific errors (I personally use https://sendgrid.com/ to send emails)

AvielNiego
  • 1,193
  • 2
  • 12
  • 27