mysql oracle python连接
2015-09-12 15:24
387 查看
#encoding:utf-8
#dataProc
#auth xiajikun
import sys
# oracle库连接模块
import cx_Oracle
# mysql库连接
import MySQLdb
import time
import os
#水电煤库
# SDM = 'username/password@ip:port/servicename'
DB_ORA_STR = 'admin/123456@10.1.1.2:1521/c9db1111'
# oracle连接
class OracleLogin(object):
#初始化,创建连接数据库对象
def __init__(self, loginName):
#--建立连接
self.conn = cx_Oracle.connect(loginName)
#--获取游标
self.cursor = self.conn.cursor()
self.l_v_status = self.cursor.var(cx_Oracle.NUMBER)
self.l_v_return_code = self.cursor.var(cx_Oracle.STRING)
self.l_v_sno = self.cursor.var(cx_Oracle.STRING)
#查询sql方法
def selectSql(self, sql):
self.cursor.execute(sql)
self.result_sql = self.cursor.fetchall()
self.count = self.cursor.rowcount
self.conn.commit()
#执行sql方法
def execSql(self,sql):
self.cursor.execute(sql)
self.conn.commit()
#执行存储过程方法
def getMetaData(self,procdName, sql, procArgs):
self.procResult = self.cursor.callproc(procdName, procArgs)
self.cursor.execute(sql)
self.result_pro = self.cursor.fetchall()
self.count = self.cursor.rowcount
self.conn.commit()
#无用时自动析构此对象
def __del__(self):
self.cursor.close()
self.conn.close()
# mysql连接
class MysqlLogin(object):
#初始化,自动连接数据库
def __init__(self, db_str):
# self.conn = MySQLdb.connect(host='10.7.11.242',user='credcard',passwd='5sFVDVCeKo',db='credcard',port=3320)
self.conn = MySQLdb.connect(host=db_str[0],user=db_str[1],passwd=db_str[2],db=db_str[3],port=db_str[4])
#self.conn = MySQLdb.connect('%s' % db_str)
#self.conn = MySQLdb.connect('%s,%s,%s,%s,%d' % (db_str)
self.cur = self.conn.cursor()
def exec_sql(self, sql):
self.cur.execute(sql)
self.conn.commit()
def select_sql(self, sql):
self.cur.execute(sql)
self.tmpdata = self.cur.fetchall()
#自动断开游标和连接
def __del__(self):
self.cur.close()
self.conn.close()
db_str = ('10.1.1.1','username','123456','db_name',3306)
db_obj = MysqlLogin(db_str)
sql = 'select * from tabname limit 10'
db_obj.select_sql(sql)
print db_obj.tmpdata
#dataProc
#auth xiajikun
import sys
# oracle库连接模块
import cx_Oracle
# mysql库连接
import MySQLdb
import time
import os
#水电煤库
# SDM = 'username/password@ip:port/servicename'
DB_ORA_STR = 'admin/123456@10.1.1.2:1521/c9db1111'
# oracle连接
class OracleLogin(object):
#初始化,创建连接数据库对象
def __init__(self, loginName):
#--建立连接
self.conn = cx_Oracle.connect(loginName)
#--获取游标
self.cursor = self.conn.cursor()
self.l_v_status = self.cursor.var(cx_Oracle.NUMBER)
self.l_v_return_code = self.cursor.var(cx_Oracle.STRING)
self.l_v_sno = self.cursor.var(cx_Oracle.STRING)
#查询sql方法
def selectSql(self, sql):
self.cursor.execute(sql)
self.result_sql = self.cursor.fetchall()
self.count = self.cursor.rowcount
self.conn.commit()
#执行sql方法
def execSql(self,sql):
self.cursor.execute(sql)
self.conn.commit()
#执行存储过程方法
def getMetaData(self,procdName, sql, procArgs):
self.procResult = self.cursor.callproc(procdName, procArgs)
self.cursor.execute(sql)
self.result_pro = self.cursor.fetchall()
self.count = self.cursor.rowcount
self.conn.commit()
#无用时自动析构此对象
def __del__(self):
self.cursor.close()
self.conn.close()
# mysql连接
class MysqlLogin(object):
#初始化,自动连接数据库
def __init__(self, db_str):
# self.conn = MySQLdb.connect(host='10.7.11.242',user='credcard',passwd='5sFVDVCeKo',db='credcard',port=3320)
self.conn = MySQLdb.connect(host=db_str[0],user=db_str[1],passwd=db_str[2],db=db_str[3],port=db_str[4])
#self.conn = MySQLdb.connect('%s' % db_str)
#self.conn = MySQLdb.connect('%s,%s,%s,%s,%d' % (db_str)
self.cur = self.conn.cursor()
def exec_sql(self, sql):
self.cur.execute(sql)
self.conn.commit()
def select_sql(self, sql):
self.cur.execute(sql)
self.tmpdata = self.cur.fetchall()
#自动断开游标和连接
def __del__(self):
self.cur.close()
self.conn.close()
db_str = ('10.1.1.1','username','123456','db_name',3306)
db_obj = MysqlLogin(db_str)
sql = 'select * from tabname limit 10'
db_obj.select_sql(sql)
print db_obj.tmpdata
相关文章推荐
- Oracle性能分析3:TKPROF简介
- 关于oracle 使用存储过程
- linux已安装Oracle未安装实例
- 对oracle实例的内存(SGA和PGA)进行调整,优化数据库性
- 使用linux内存HugePages优化oracle内存区
- Oracle中的PLsql的符号解释大全
- Oracle 分页存储过程
- 安装oracle10g 出现oui.exe停止工作错误
- 查询表上的约束
- Oracle基本概念
- oracle中lock和latch的用途
- oracle中awr性能报告阅读笔记
- Redhat Enterprise Linux6.5 静默安装 Oracle 11gR2 RAC
- PL/SQL联系oracle成功可以sql解决的办法是检查表的名称无法显示
- Oracle sql 优化の常用方式
- oracle新建用户及授权
- 解决X64操作系统PL/SQL连接报错问题 make sure you have the 32 bits oracle client installed
- Oracle触发器创建自增主键
- oracle中删除表空间提示ORA-00604和ORA-38301
- 下载安装oracle11g