一、封装方式一

#encoding:utf-8
import cx_Oracle
class Oracle_Status_Output:
    def __init__(self,db_name,db_password,db_tns):
        try:
            self.db = cx_Oracle.connect(db_name,db_password,db_tns)
            self.cursor = self.db.cursor()
        except Exception as e:
            print('Wrong')
            print(e)
    def oracle_status_select(self,sql):
        try:
            self.cursor.execute(sql)
            col=col=self.cursor.description
            v_result=self.cursor.fetchall()
            return v_result,col
        except Exception as e:
            print(e)
    def oracle_status_dml(self,sql):
        try:
            self.cursor.execute(sql)
            self.db.commit()
            print("DML OK")
        except Exception as e:
            print(e)
    def close(self):
        self.cursor.close()
        self.db.close()

二、封装方式二

#  coding=utf-8

import cx_Oracle
import os
import json

os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
"""python version 3.7"""

class TestOracle(object):
    def __init__(self, user, pwd, ip, port, sid):
        self.connect = cx_Oracle.connect(user + "/" + pwd + "@" + ip + ":" + port + "/" + sid)
        self.cursor = self.connect.cursor()

def select(self, sql):
        list = []
        self.cursor.execute(sql)
        result = self.cursor.fetchall()
        col_name = self.cursor.description
        for row in result:
            dict = {}
            for col in range(len(col_name)):
                key = col_name[col][0]
                value = row[col]
                dict[key] = value
            list.append(dict)
        js = json.dumps(list, ensure_ascii=False, indent=2, separators=(',', ':'))
        return js

def disconnect(self):
        self.cursor.close()
        self.connect.close()

def insert(self, sql, list_param):
        try:
            self.cursor.executemany(sql, list_param)
            self.connect.commit()
            print("插入ok")
        except Exception as e:
            print(e)
        finally:
            self.disconnect()

def update(self, sql):
        try:
            self.cursor.execute(sql)
            self.connect.commit()

except Exception as e:
            print(e)
        finally:
            self.disconnect()

def delete(self, sql):
        try:
            self.cursor.execute(sql)
            self.connect.commit()
            print("delete ok")
        except Exception as e:
            print(e)
        finally:
            self.disconnect()

Python Oracle连接与操作封装的更多相关文章

  1. Python 使用Python远程连接并操作InfluxDB数据库

    使用Python远程连接并操作InfluxDB数据库 by:授客 QQ:1033553122 实践环境 Python 3.4.0 CentOS 6 64位(内核版本2.6.32-642.el6.x86 ...

  2. 使用python简单连接并操作数据库

    python中连接并操作数据库 图示操作流程 一.使用的完整流程 # 1. 导入模块 from pymysql import connect # 2. 创建和数据库服务器的连接,自行设置 服务器地址, ...

  3. Python 如何连接并操作 Aws 上 PB 级云数据仓库 Redshift

    Python 如何连接并操作 Aws 上 PB 级云数据仓库 Redshift 一.简介 Amazon Redshift 是一个快速.可扩展的数据仓库,可以简单.经济高效地分析数据仓库和数据湖中的所有 ...

  4. 关于python字符串连接的操作

    python字符串连接的N种方式 注:本文转自http://www.cnblogs.com/dream397/p/3925436.html 这是一篇不错的文章 故转 python中有很多字符串连接方式 ...

  5. ORACLE连接SQLSERVER

    一.实验(实验成功) 1.实验目标:ORACLE连接SQLSERVER以及查询数据 2.搭建的环境: oracle 9i 9.0.2.0.1 地址:192.168.40.139 sql2000 的数据 ...

  6. 随笔记:如何使用Python连接(/操作)Oracle数据库(Windows平台下)

    遇到需求,我们需要用Python对Oracle数据库进行操作. 这次我们使用cx_Oracle Oracle Client 在安装cx_Oracle之前,先安装Oracle客户端. cx_Oracle ...

  7. python - DBUtils 连接池减少oracle数据库的连接数

    问题: 接到需求,告知项目的oracle连接次数过多,对系统造成太过大的负担,要求减少oracle数据库的连接次数 分析: 仔细分析代码以后,发现产生问题的原因,在于之前要求提升oracle监控的监控 ...

  8. C#连接和操作Oracle数据

    最近业务需要读取远程Oracle数据库的数据,这里简单记录一下. 这里采用的是Oracle.ManagedDataAccess方式连接Oracle数据库,这种方式有几个优点:①不用安装Oracle客户 ...

  9. Java java jdbc thin远程连接并操作Oracle数据库

    JAVA jdbc thin远程连接并操作Oracle数据库 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 编码工具:Eclipse 编码平台:W ...

随机推荐

  1. ionic service

    当你初试 Angular 时,很自然地就会往 controller 和 scope 里堆满不必要的逻辑.一定要早点意识到,controller 这一层应该很薄:也就是说,应用里大部分的业务逻辑和持久化 ...

  2. tcpdump使用方法总结

    举例: 1.针对指定网卡eth0抓包 tcpdump -i eth0 2.过滤主机 tcpdump -i eth0 host 192.168.1.1 tcpdump -i eth0 src host ...

  3. python实战小程序之购物车

    # Author:南邮吴亦凡 # 商品列表 product_list = [ ('Iphone',5800), # 逗号一定不可以省略! ('Mac',4800), ('smartphone',400 ...

  4. centos7:mysql-5.7.23安装(二进制安装)

    mysql有二进制码安装,和源码编译安装(mysql5.5使用cmake安装,mysql5.7需要安装boost依赖安装),因为boost依赖安装麻烦,所以用二进制码安装 MySql 5.7.23安装 ...

  5. C#异步的世界(重点:新异步)

    http://www.cnblogs.com/zhaopei/p/async_two.html

  6. 利用Vistual Studio自带的xsd.exe工具,根据XML自动生成XSD

    利用Vistual Studio自带的xsd.exe工具,根据XML自动生成XSD 1, 命令提示符-->找到vs自带的xsd.exe工具所在的文件夹 例如: C:\Program Files ...

  7. c++的const总结

    转自:http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777416.html 为什么使用const?采用符号常量写出的代码更容易维护:指 ...

  8. vue 基础(二)

    Vue对象提供的属性功能 一.过滤器 过滤器,就是vue允许开发者自定义的文本格式化函数,可以使用在两个地方:输出内容和操作数据中. 1. 全局过滤器 Vue.filter 写在vm 对象外.必须要有 ...

  9. Single Number leetcode java

    问题描述: Given an array of integers, every element appears twice except for one. Find that single one. ...

  10. 20165309 实验四 Android程序设计

    2017-2018-2 20165309实验四<Java面向对象程序设计>实验报告 一.实验内容 1.Android Studio的安装测试 2.Activity测试 3.UI测试 4.布 ...