在Python中,使用cx_Oracle库连接Oracle数据库时,如果出现ORA-xxxxx错误,可以通过以下方法获取错误号:
cursor.fetchall()
方法获取查询结果时,如果出现错误,可以通过cursor.errorinfo
属性获取错误号。import cx_Oracle
dsn = cx_Oracle.makedsn('host', 'port', service_name='service_name')
conn = cx_Oracle.connect(user='username', password='password', dsn=dsn)
cursor = conn.cursor()
try:
cursor.execute('SELECT * FROM table_name')
rows = cursor.fetchall()
except cx_Oracle.DatabaseError as e:
error_code = e.args[0].code
print(f"Error code: {error_code}")
cursor.execute()
方法的返回值获取错误号。import cx_Oracle
dsn = cx_Oracle.makedsn('host', 'port', service_name='service_name')
conn = cx_Oracle.connect(user='username', password='password', dsn=dsn)
cursor = conn.cursor()
try:
cursor.execute('INSERT INTO table_name (column1, column2) VALUES (value1, value2)')
conn.commit()
except cx_Oracle.DatabaseError as e:
error_code = e.args[0].code
print(f"Error code: {error_code}")
需要注意的是,cx_Oracle库的错误号与Oracle数据库的错误号是不同的,因此需要查阅cx_Oracle库的官方文档来获取具体的错误信息。
领取专属 10元无门槛券
手把手带您无忧上云