python 取oracle数据转存至mysql

由于grafana的oracle插件需要付费,所以只能想想办法,于是就用Oracle的数据转到mysql数据库里面。

其实也很简单,需要提前安装好python和oracle数据库驱动cx_oracle 和MySQL 驱动,具体可以自己搜索。

脚本如下

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import cx_Oracle #导入包
import MySQLdb 
import sys
import logging
#解决中文乱码
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
#os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.AL32UTF8'

def Oracle_cnn():
tmp = read_conf(2)
try:
db = cx_Oracle.connect(tmp[1],tmp[2],tmp[3]) #连接数据库
print "oracle connect ok~"
cur = db.cursor() # 游标操作
cur.execute('select * from cnt') # 执行sql语句
rows = cur.fetchall() # 获取数据
list = rows
otmp = "oracle select:"+str(len(list))
log_print(otmp)
cur.close()
db.close()
return list
# 打印数据
# print str(list).decode('string_escape')

#read conf
def read_conf(tmp):
if(tmp == 1):
file_object = open('/root/py/mysql.conf')
try:
msql = file_object.readlines( )
msql = deal_list(msql)
return msql
except Exception,e:
print e
log_print(e)
file_object.close( )
if(tmp == 2):
file_object = open('/root/py/oracle.conf')
try:
osql = file_object.readlines( )
osql = deal_list(osql)
return osql
except Exception,e:
print e
log_print(e)
file_object.close( )

#deal the list
def deal_list(list):
# print list
for line in range(len(list)):
list[line]=list[line].strip('\n')
if(list[line] == ''):
list.remove('')
return list

Mysql_cnn()

因为我把mysql 和 Oracle 链接相关数据存在conf配置文件中。具体如图

猜你喜欢

转载自www.cnblogs.com/lc226/p/11442352.html