python 操作oracle 简单封装

2018-01-16 12:41:07来源:网络收集作者:管理员人点击

分享

阿里云爆款

需要用python操作oracle ,对cx_oracle的函数做了进一步封装,代码如下:


deal_oracle.py:


#!/usr/bin/env python
#coding=utf-8
import cx_Oracle as Oracle
class DealOracle:
    def __init__(self,oracle_login):
        self.oracle_login = oracle_login
        self.con = None
    def connectdb(self):
        if self.con == None:
            self.con = Oracle.connect(self.oracle_login)
        if self.con != None:
            return True
        else:
            return False
    def closedb(self):
        if self.con:
            self.conn. close()
            self.conn = None
    def newcur(self):
        cur = self.con.cursor()
        if cur:
            return cur
        else:
            print ("#Error# Get New Cursor Failed.")
            return None
    def closecur(self,cur):
        if cur:
            cur.close()
    def commit(self):
        self.con.commit()
    def rollback(self):
        self.con.rollback()
    def select_sql(self,sql,cur):
        c = cur.execute(sql)
        result = c.fetchall()
        return result
    def exec_sql(self,sql,cur,parm_list = None):
        if parm_list:
            cur.prepare(sql)
            result = cur.executemany(None,parm_list)
        else:
            result = cur.execute(sql)
        return result

oracle_demo.py:


#coding=utf-8
from sys import path
path.append('../class')
import deal_oracle
if __name__ == "__main__":
    user=''
    passwd=''
    tns=''
    i=0
    fp=open("../data/database.cfg",'r')
    try:
        for line in fp.readlines():
            line=line.strip('/n')
            if line.split('=',1)[0] == 'user':
                user=line.split('=',1)[1]
            elif line.split('=',1)[0] == 'passwd':
                passwd=line.split('=',1)[1]
            elif line.split('=',1)[0] == 'tns':
                tns=line.split('=',1)[1]
    finally:
        fp.close()
    login=user + '/' + passwd + '@' + tns
    myOracle = deal_oracle.DealOracle(login)
    myOracle.connectdb()
    #select
    '''
    cur = myOracle.newcur()
    result = myOracle.select_sql("select serv_id from serv where serv_id in(60033017564,10002768220,60002665838)",cur)
    myOracle.closecur(cur)
    print(result)
    for i in result:
        print(i)
    '''
    #insert
    '''
    aa = []
    for i in 60033017564,10002768220,60002665838:
        aa.append((60033017564,))
        print(aa)
    cur = myOracle.newcur()
    result = myOracle.exec_sql("insert into zhangbo_serv(serv_id) values(:1)",cur,aa)
    myOracle.commit()
    myOracle.closecur(cur)
    '''
    #update
    '''
    cur = myOracle.newcur()
    result = myOracle.exec_sql("update zhangbo_serv set serv_id = 1111",cur)
    myOracle.commit()
    myOracle.closecur(cur)
    '''
    M = []
    for i in 1111,11111:
        M.append((i,))
    cur = myOracle.newcur()
    result = myOracle.exec_sql("update zhangbo_serv set serv_id = 222 where serv_id = :1",cur,M)
    myOracle.commit()
    myOracle.closecur(cur)



最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台