首页 > 数据库技术 > 详细

python 取oracle数据转存至mysql

时间:2019-09-01 17:17:58      阅读:203      评论:0      收藏:0      [点我收藏+]

由于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配置文件中。具体如图

技术分享图片

 

python 取oracle数据转存至mysql

原文:https://www.cnblogs.com/lc226/p/11442352.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!