转载

ubuntu server下python脚本迁移数据

因为是在Ubuntu下,所以安装python、pip、pymysql等都极其方便,sudo apt-get install pymysql,

但是在安装cx_Oracle(连接oracle的模块)出现许多问题,查阅相关资料,发现这边文章能够帮我解决,希望大家少走点弯路。 http://www.tbdazhe.com/archives/602

1.安装python

2.安装pip、pymysql、cx_oracle

3.import module

主要这次我想通过python脚本,定时把数据从oracle库导入mysql库

__author__ = 'young' # coding=UTF-8 import os os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' import cx_Oracle # 导入pymysql的包 import pymysql def synctask():  '''Hello cx_Oracle示例:  1)打印数据库版本信息.  2)查询表数据.'''  conn = cx_Oracle.connect("username/password@ip:1522/health")  cur = conn.cursor()  # 获取一个数据库连接,注意如果是UTF-8类型的,需要制定数据库  conn_mysql = pymysql.connect(host='ip', user='smart', passwd='smart', db='smart', port=3306,          charset='utf8')  cur_mysql = conn_mysql.cursor()  # 获取一个游标  try:   cur_mysql.execute("insert into user(name) values('123')")   print "Oracle Version:%s" % conn.version   print "Table test rows:"   sql = 'SELECT *  FROM t_user where pat_id < 2000 order by pat_id'   cur.execute(sql)   for row in cur:    print row[1].decode('utf-8')    data = row[1].decode('utf-8')  finally:   conn_mysql.commit()   conn.commit()   conn_mysql.close()   cur_mysql.close()   cur.close()   conn.close() synctask() 
正文到此结束
Loading...