Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cx_Oracle
- import time
- user = 'myuser'
- psw = 'admin'
- host = '165.45.12.1'
- name = 'db'
- table = "mytabla"
- con = None
- cursor = None
- try:
- con, cursor = reconectar(con, cursor, user, psw, host, name)
- while True:
- if cursor is not None:
- try:
- query = f'SELECT * FROM {table}'
- cursor.execute(query)
- result = cursor.fetchall()
- print(result)
- print(con)
- print(cursor)
- except (cx_Oracle.DatabaseError, cx_Oracle.InterfaceError) as ex:
- print(f'Excepcion: {ex}')
- print(con)
- print(cursor)
- error, = ex.args
- if error.code == 3135:
- con, cursor = reconectar(con, cursor, user, psw, host, name)
- time.sleep(20)
- except KeyboardInterrupt:
- exit(0)
- def reconectar(con, cursor, user, psw, host, name):
- try:
- cursor.close()
- except Exception:
- print("Cursor ya cerrado")
- try:
- con.close()
- except Exception:
- print("Conexión ya cerrada")
- try:
- con = cx_Oracle.connect(user+'/'+psw+'@'+host+'/'+name)
- except Exception:
- print("Fallo al conectar")
- con = None
- try:
- cursor = con.cursor()
- except Exception:
- print("Fallo al crear cursor")
- cursor = None
- return con, cursor
- Cursor ya cerrado
- Conexión ya cerrada
- [(2, 5, 5, 5, 2, 10, 3, 3, 30, 10, 30, 60, 30, 30, 30, 30, 10.0)]
- <cx_Oracle.Connection to myuser@165.45.12.1/db>
- <cx_Oracle.Cursor on <cx_Oracle.Connection to myuser@165.45.12.1/db>>
- Excepcion: ORA-03135: la conexión ha perdido contacto
- Identificador de Proceso: 0
- Identificador de Sesión: 842 Número de Serie: 98425
- <cx_Oracle.Connection to myuser@165.45.12.1/db>
- <cx_Oracle.Cursor on <cx_Oracle.Connection to myuser@165.45.12.1/db>>
- [(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