本文實例講述了python使用 cx_Oracle 模塊進行查詢操作。分享給大家供大家參考,具體如下:
# !/usr/bin/env python# -*- coding: utf-8 -*-import cx_Oraclefrom pprint import pprintimport csvimport timeimport reimport binasciiprint time.ctime()try: conn = cx_Oracle.connect('tlcbuser/[email protected]/tlyy')# cursor = conn.cursor()# xsql="select * from tlcb_mon_device a where a.ipaddr='10.3.244.1'"# r = cursor.execute(xsql)# print rexcept Exception,e: print e print type(e) print str(e).decode('UTF-8').encode('GBK') print time.ctime()
C:/Python27/python.exe C:/Users/tlcb/PycharmProjects/untitled/a6.py
Mon Oct 22 10:35:59 2018
ORA-12170: TNS: ���ӳ�ʱ
<class 'cx_Oracle.DatabaseError'>
Mon Oct 22 10:36:20 2018
Process finished with exit code 0
# !/usr/bin/env python# -*- coding: utf-8 -*-import cx_Oraclefrom pprint import pprintimport csvimport timeimport reimport binasciiimport osos.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'print time.ctime()try: conn = cx_Oracle.connect('tlcbuser/[email protected]/tlyy')# cursor = conn.cursor()# xsql="select * from tlcb_mon_device a where a.ipaddr='10.3.244.1'"# r = cursor.execute(xsql)# print rexcept Exception,e: print e print type(e) print time.ctime()
C:/Python27/python.exe C:/Users/tlcb/PycharmProjects/untitled/a6.py
Mon Oct 22 10:44:20 2018
ORA-12170: TNS: 連接超時
<class 'cx_Oracle.DatabaseError'>
Mon Oct 22 10:44:41 2018
Process finished with exit code 0
# !/usr/bin/env python# -*- coding: utf-8 -*-import cx_Oraclefrom pprint import pprintimport csvimport timeimport reimport binasciiimport osos.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'print time.ctime()conn = cx_Oracle.connect('test/[email protected]/serv')cursor = conn.cursor()xsql="select 'aaa' from dual"cursor.execute(xsql)result = cursor.fetchall()print result
C:/Python27/python.exe C:/Users/tlcb/PycharmProjects/untitled/rizhiyi/a7.py
Mon Oct 22 11:33:52 2018
[('aaa',)]
Process finished with exit code 0
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python常見數據庫操作技巧匯總》、《Python編碼操作技巧總結》、《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答