python操作oracle数据库

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

# -*- mode: python; coding: utf-8 -*-
#
# python operate oracle,contain insert、delete、update、select.
#
# @author liyulin  
# @date 2014-11-07  

import cx_Oracle

class PythonoralceUtil:
    def __enter__(self):
        self.conn = cx_Oracle.connect('testuser/[email protected]/CHROMEBOOK') 
        self.cursor = self.conn.cursor()
        return self 
  
    def __exit__(self,type,value,traceback): 
        self.cursor.close()
        self.conn.close()
        
    ############################################
    # 查询reg_codes中的所有数据
    ############################################
    def queryAll(self):
        self.cursor.execute('select * from reg_codes')
        results = self.cursor.fetchall()
        for result in results:
            print result
    
    ############################################
    # 根据序号查询reg_codes中的一条数据
    ############################################
    def queryBySeq(self,seq):
        self.cursor.execute('select * from reg_codes where seq=:1',seq)
        result = self.cursor.fetchone()
        if (result is not None):         
            for index in range(0,6):
                print result[index],############################################
    # 向reg_codes中插入N条数据
    ############################################
    def insertManay(self,insertValue):
        self.conn.begin()
        try:
            self.cursor.executemany('insert into reg_codes(device,unique_code,group_code,input_file,sn,input_ts) values(:1,:2,:3,:4,:5,sysdate)',insertValue)
        except AssertionError:
            self.conn.rollback()
            raise Warning,"invalid insertValue (%s)" % insertValue
        self.conn.commit()
        
    ############################################
    # 更新reg_codes中一条数据
    ############################################
    def updateOne(self,sqe,input_file):
        updateValue = [input_file,sqe]
        self.cursor.execute('update reg_codes set input_file=:1 where seq=:2',updateValue)
    
    ############################################
    # 更新reg_codes中N条数据
    ############################################
    def updateManay(self,updateValues):
        self.conn.begin()
        try:
            self.cursor.executemany('update reg_codes set input_file=:1 where seq=:2',updateValues)
        except AssertionError:
            self.conn.rollback()
            raise Warning,"invalid insertValue (%s)" % updateValues
        self.conn.commit()
    
    ############################################
    # 删除reg_codes中一条数据
    ############################################
    def delete(self,sqe):
        self.cursor.execute('delete from reg_codes where seq=:1',sqe)
    
    ############################################
    # 删除reg_codes中N条数据
    ############################################
    def deleteManay(self,seqs):
        self.conn.begin()
        try:
            self.cursor.executemany('delete from reg_codes where seq=:1',seqs)
        except AssertionError:
            self.conn.rollback()
            raise Warning,"invalid seqs (%s)" % seqs
        self.conn.commit()
            
############################################
# 执行代码
############################################        
with PythonoralceUtil() as pythonoralceUtil:
#     insertValue = [['jerry','unique_code2333','group_code2333','debug233','1111111111122'],#                ['jerry','unique_code244','group_code244','debug244','22222222233'],'unique_code255','group_code255','debug255','33333333344'],'unique_code266','group_code266','debug266','44444444455'],'unique_code277','group_code277','debug277','55555555566']]
#     pythonoralceUtil.insertManay(insertValue)
#     pythonoralceUtil.updateOne('27','debug_updated')
#     pythonoralceUtil.delete([27])
#     pythonoralceUtil.deleteManay([[31],[44],[45]])
    updateValues = [['debug_updated','46'],['debug_updated','47'],'48'],'34']]
    pythonoralceUtil.updateManay(updateValues)
    pythonoralceUtil.queryAll()
    pythonoralceUtil.queryBySeq([27])

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...