2016-09-23 8 views
0

오늘 프로그래밍 된 스크립트가 실행 중일 때 데이터베이스가 다운 된 문제가 있습니다. 이 예외가 다시 나에게 이메일을 보내려면 (나는 이미 그것을 할 함수를 생성)을 만들 때 싶습니다. 하지만 그것에 대한 예외를 만들려고하는데 실패하고 있습니다.Python : pymysql을 사용하여 거부 된 데이터베이스 연결에 대한 예외 생성

import pymysql.cursors 

    #Conection Settings.... 

    try: 

     with connection.cursor() as cursor: 
      # Read a single record 
      sql = "select count(*) as total_invoices, " \ 
        "sum(e.Montant_vente - e.Montant_tva_recup) as total_amount " \ 
        "from extrait_sapeig_stat e " \ 
        "where e.montant_vente != 0 " \ 
        "and e.numero_client = 1086 " \ 
        "and e.activite != 11 " \ 
        "and e.invoice_date = date_add(curdate(), Interval -1 DAY) " \ 
        "and right(e.Numero_facture,7) not in (1182499) " \ 


      print(sql) 
      cursor.execute(sql) 
      inv_counts_2 = cursor.fetchall() 


    finally: 
     connection.close() 
+0

'OperationalError'를 잡아야합니다. 그리고 거기에 당신의 논리를 넣어 라. – sisanared

답변

0

예외를 정의하지 않았기 때문에.

import pymysql.cursors 

    #Connection Settings.... 

    try: 

     with connection.cursor() as cursor: 
      # Read a single record 
      sql = "select count(*) as total_invoices, " \ 
        "sum(e.Montant_vente - e.Montant_tva_recup) as total_amount " \ 
        "from extrait_sapeig_stat e " \ 
        "where e.montant_vente != 0 " \ 
        "and e.numero_client = 1086 " \ 
        "and e.activite != 11 " \ 
        "and e.invoice_date = date_add(curdate(), Interval -1 DAY) " \ 
        "and right(e.Numero_facture,7) not in (1182499) " \ 


      print(sql) 
      cursor.execute(sql) 
      inv_counts_2 = cursor.fetchall() 
    except Exception as e: 
     print (e) 

    finally: 
     connection.close()