This article example describes python query operation using cx_Oracle module. Shared for your reference, as follows:
# !/usr/bin/env python # -*- coding: utf-8 -*- import cx_Oracle from pprint import pprint import csv import time import re import binascii print () try: conn = cx_Oracle.connect('tlcbuser/[email protected]/tlyy') # cursor = () # xsql="select * from tlcb_mon_device a where ='10.3.244.1'" # r = (xsql) # print r except Exception,e: print e print type(e) print str(e).decode('UTF-8').encode('GBK') print ()
C:\Python27\ C:/Users/tlcb/PycharmProjects/untitled/
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 ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' print () try: conn = cx_Oracle.connect('tlcbuser/[email protected]/tlyy') # cursor = () # xsql="select * from tlcb_mon_device a where ='10.3.244.1'" # r = (xsql) # print r except Exception,e: print e print type(e) print ()
C:\Python27\ C:/Users/tlcb/PycharmProjects/untitled/
Mon Oct 22 10:44:20 2018
ORA-12170: TNS: connection timeout
<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 ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' print () conn = cx_Oracle.connect('test/[email protected]/serv') cursor = () xsql="select 'aaa' from dual" (xsql) result = () print result
C:\Python27\ C:/Users/tlcb/PycharmProjects/untitled/rizhiyi/
Mon Oct 22 11:33:52 2018
[('aaa',)]
Process finished with exit code 0
Readers interested in more Python related content can check out this site's topic: theSummary of common database manipulation techniques in Python》、《Summary of Python coding manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Python Socket Programming Tips Summary》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques》
I hope that the description of this article will be helpful for you Python Programming.