python 迁移数据
程序员文章站
2022-06-03 13:57:46
...
#!/usr/bin/python2
# -*- coding:utf-8 -*-
#
import MySQLdb
# 原始数据的数据连接
sourceConn=MySQLdb.connect(XXX)
sourceCur=sourceConn.cursor()
# 定义查询语句
len = sourceCur.execute('')
# 目标库的数据连接
targetConn=MySQLdb.connect(XXX)
targetCur=targetConn.cursor()
# 批量插入语句
sql = ''
# 每次循环导入的数据量
num = 100
try:
for i in range(int(len/num)+1):
print(i)
data = sourceCur.fetchmany(num)
targetCur.executemany(sql, data)
targetConn.commit()
except Exception as e:
targetConn.rollback()
print(e)
print("Error: unable to fetch data")
# 关闭数据库连接
sourceConn.close()
targetConn.close()