zoukankan      html  css  js  c++  java
  • Python Oracle连接与操作封装

    一、封装方式一

    #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()


  • 相关阅读:
    Tomcat8 配置Oracle11g数据源
    通过代码设置 为横屏
    HttpClient4.3.6 实现https访问
    创建mysql数据库
    设置android状态栏颜色和toolbar颜色一致
    定义任务打印gradle下载的jar包位置
    修改weblogic jvm启动参数
    android.support.v7.widget.Toolbar 中menu图标不显示问题
    android.support.design.widget.AppBarLayout 在android5.0+底部显示空白条问题
    浅析jQuery框架与构造对象
  • 原文地址:https://www.cnblogs.com/xibuhaohao/p/10570048.html
Copyright © 2011-2022 走看看