一、封装方式一

#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. 如何调节tomcat初始内存

    1.linux下调节tomcat初始内存大小 linux下tomcat的运行文件为catalina.sh,打开文件,在文件靠近顶部找到“ JAVA_OPTS ”字样,在它后面添加如下内容即可 # JA ...

  2. windows下开启端口映射配置办法

    #1.添加一个端口映射 netsh interface portproxy add v4tov4 listenaddress=大网IP listenport=端口  connectaddress=要映 ...

  3. python3-知识扩展扫盲易忘-map,collections.Counter()的用法

    map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表. >> ...

  4. android ------- TCP与UDP

    TCP TCP(Transmission Control Protocol,传输控制协议) 即传输控制协议,是一种传输层通信协议 特点:面向连接.面向字节流.全双工通信.可靠 面向连接:指的是要使用T ...

  5. 手机中SN、MEID、IMEI的意思

    SN SN码是Serial Number的缩写,有时也叫SerialNo,也就是产品序列号,产品序列是为了验证“产品的合法身份”而引入的一个概念,它是用来保障用户的正版权益,享受合法服务的:一套正版的 ...

  6. LOJ6072苹果树

    虽然结合了很多算法,但是一步一步地推一下还不算太难的一道题. 首先考虑枚举枚举有用的苹果的集合,然后去算生成树个数. 先考虑怎么计算生成树个数. 发现可以使用matrix-tree. 所有有用点可以和 ...

  7. fiddler 显示server ip

    Fiddler显示服务器ip地址列(方便查看host是否生效) 2016年08月31日 15:40:10 阅读数:5801 1.点击菜单栏rules——customize rules... 2.ctr ...

  8. New task CodeForces - 788E (线段树优化dp)

    比较套路的一个题, 对每个数维护一颗线段树来转移就好了. #include <iostream> #include <algorithm> #include <cstdi ...

  9. c 语言连续输入字符型数据

    #include<stdio.h> #include<stdlib.h> void Input1(char* &str){ // /* 这种情况下想要逐个输入字符串数组 ...

  10. Analytic Functions in Oracle

    Contents Overview and IntroductionHow Analytic Functions WorkThe SyntaxExamplesCalculate a running T ...