1. 程式人生 > 資料庫 >python使用 cx_Oracle 模組進行查詢操作示例

python使用 cx_Oracle 模組進行查詢操作示例

本文例項講述了python使用 cx_Oracle 模組進行查詢操作。分享給大家供大家參考,具體如下:

# !/usr/bin/env python
# -*- coding: utf-8 -*-
import cx_Oracle
from pprint import pprint
import csv
import time
import re
import binascii
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 r
except 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_Oracle
from pprint import pprint
import csv
import time
import re
import binascii
import os
os.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 r
except 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_Oracle
from pprint import pprint
import csv
import time
import re
import binascii
import os
os.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程式設計有所幫助。