Guest User

Untitled

a guest
Jul 23rd, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import cx_Oracle
  2. import time
  3.  
  4. user = 'myuser'
  5. psw = 'admin'
  6. host = '165.45.12.1'
  7. name = 'db'
  8. table = "mytabla"
  9.  
  10. con = None
  11. cursor = None
  12.  
  13. try:
  14.  
  15. con, cursor = reconectar(con, cursor, user, psw, host, name)
  16.  
  17. while True:
  18.  
  19. if cursor is not None:
  20. try:
  21. query = f'SELECT * FROM {table}'
  22. cursor.execute(query)
  23. result = cursor.fetchall()
  24. print(result)
  25. print(con)
  26. print(cursor)
  27. except (cx_Oracle.DatabaseError, cx_Oracle.InterfaceError) as ex:
  28. print(f'Excepcion: {ex}')
  29. print(con)
  30. print(cursor)
  31. error, = ex.args
  32. if error.code == 3135:
  33. con, cursor = reconectar(con, cursor, user, psw, host, name)
  34.  
  35. time.sleep(20)
  36. except KeyboardInterrupt:
  37. exit(0)
  38.  
  39. def reconectar(con, cursor, user, psw, host, name):
  40.  
  41. try:
  42. cursor.close()
  43. except Exception:
  44. print("Cursor ya cerrado")
  45.  
  46. try:
  47. con.close()
  48. except Exception:
  49. print("Conexión ya cerrada")
  50.  
  51. try:
  52. con = cx_Oracle.connect(user+'/'+psw+'@'+host+'/'+name)
  53. except Exception:
  54. print("Fallo al conectar")
  55. con = None
  56.  
  57. try:
  58. cursor = con.cursor()
  59. except Exception:
  60. print("Fallo al crear cursor")
  61. cursor = None
  62.  
  63. return con, cursor
  64.  
  65. Cursor ya cerrado
  66. Conexión ya cerrada
  67. [(2, 5, 5, 5, 2, 10, 3, 3, 30, 10, 30, 60, 30, 30, 30, 30, 10.0)]
  68. <cx_Oracle.Connection to myuser@165.45.12.1/db>
  69. <cx_Oracle.Cursor on <cx_Oracle.Connection to myuser@165.45.12.1/db>>
  70. Excepcion: ORA-03135: la conexión ha perdido contacto
  71. Identificador de Proceso: 0
  72. Identificador de Sesión: 842 Número de Serie: 98425
  73. <cx_Oracle.Connection to myuser@165.45.12.1/db>
  74. <cx_Oracle.Cursor on <cx_Oracle.Connection to myuser@165.45.12.1/db>>
  75. [(2, 5, 5, 5, 2, 10, 3, 3, 30, 10, 30, 60, 30, 30, 30, 30, 10.0)]
Add Comment
Please, Sign In to add comment