Python 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连接与操作封装的更多相关文章
- Python 使用Python远程连接并操作InfluxDB数据库
使用Python远程连接并操作InfluxDB数据库 by:授客 QQ:1033553122 实践环境 Python 3.4.0 CentOS 6 64位(内核版本2.6.32-642.el6.x86 ...
- 使用python简单连接并操作数据库
python中连接并操作数据库 图示操作流程 一.使用的完整流程 # 1. 导入模块 from pymysql import connect # 2. 创建和数据库服务器的连接,自行设置 服务器地址, ...
- Python 如何连接并操作 Aws 上 PB 级云数据仓库 Redshift
Python 如何连接并操作 Aws 上 PB 级云数据仓库 Redshift 一.简介 Amazon Redshift 是一个快速.可扩展的数据仓库,可以简单.经济高效地分析数据仓库和数据湖中的所有 ...
- 关于python字符串连接的操作
python字符串连接的N种方式 注:本文转自http://www.cnblogs.com/dream397/p/3925436.html 这是一篇不错的文章 故转 python中有很多字符串连接方式 ...
- ORACLE连接SQLSERVER
一.实验(实验成功) 1.实验目标:ORACLE连接SQLSERVER以及查询数据 2.搭建的环境: oracle 9i 9.0.2.0.1 地址:192.168.40.139 sql2000 的数据 ...
- 随笔记:如何使用Python连接(/操作)Oracle数据库(Windows平台下)
遇到需求,我们需要用Python对Oracle数据库进行操作. 这次我们使用cx_Oracle Oracle Client 在安装cx_Oracle之前,先安装Oracle客户端. cx_Oracle ...
- python - DBUtils 连接池减少oracle数据库的连接数
问题: 接到需求,告知项目的oracle连接次数过多,对系统造成太过大的负担,要求减少oracle数据库的连接次数 分析: 仔细分析代码以后,发现产生问题的原因,在于之前要求提升oracle监控的监控 ...
- C#连接和操作Oracle数据
最近业务需要读取远程Oracle数据库的数据,这里简单记录一下. 这里采用的是Oracle.ManagedDataAccess方式连接Oracle数据库,这种方式有几个优点:①不用安装Oracle客户 ...
- Java java jdbc thin远程连接并操作Oracle数据库
JAVA jdbc thin远程连接并操作Oracle数据库 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 编码工具:Eclipse 编码平台:W ...
随机推荐
- Netty实现简易http_server
Netty可以通过一些handler实现简单的http服务器.具体有三个类,分别是HttpServer.java.ServerHandlerInit.java.BusiHandler.java. 具体 ...
- 目前用到的一些os.path方法
这里主要记录下os.path.join()的用法 目录结构如下 在readconfig.py中进行试验,如下 1.使用os.path.realpath(__file__)获取文件所在目录 import ...
- Jenkins与Gitlab集成
一.安装jenkinshttps://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat/ #清华yum源 yum -y install java-1.8. ...
- WCF服务寄宿IIS与Windows服务 - C#/.NET
WCF是Windows平台下程序间通讯的应用程序框架.整合和 .net Remoting,WebService,Socket的机制,是用来开发windows平台上分布式开发的最佳选择.wcf程序的运行 ...
- Vue.js示例:树型视图; 模式组件;
树型图 本示例是一个简单的树形视图实现,它展现了组件的递归使用. mycode pen:https://codepen.io/chentianwei411/pen/KGKQxE 重点:递归是如何形成的 ...
- laravel项目数据库交互逻辑
一般在获取数据库数据的时候,我们会使用get().或者first()来获取数据,但是在做一个项目的时候我使用了first()->toArray(),然后就报错了,鉴于此就好好的研究了get和fi ...
- link标签实现给网页标题前加一个小图标favicon.ico
使用方法如下:1.<link rel="shortcut icon " type="images/x-icon" href="./favicon ...
- ccf碰撞的小球
之前的代码有人运行不成功,自己又看了一下是输入变量顺序输入错了,现在是正确答案- #include<stdio.h> struct node{ int x; int dir; }; int ...
- PAT 1027 Colors in Mars
1027 Colors in Mars (20 分) People in Mars represent the colors in their computers in a similar way ...
- 22. Generate Parentheses C++回溯法
把左右括号剩余的次数记录下来,传入回溯函数. 判断是否得到结果的条件就是剩余括号数是否都为零. 注意判断左括号是否剩余时,加上left>0的判断条件!否则会memory limited erro ...