最近得到了一个任务,让我将旧的数据库 数据迁移到新的数据库
这里用Python实现了思路 大致思路是这样 根据具体需要去更改代码‘
# -*- coding: UTF-8 -*-
import MySQLdb as mdb
conn = mdb.connect(
host="127.0.0.1",
port=3306,
user = 'root',
passwd="1234",
db="test"
)
conn2 = mdb.connect(
host="127.0.0.1",
port=3306,
user='root',
passwd="1234",
db="test",#new databases1 to save conetent
)
conn3 = mdb.connect(
host="127.0.0.1",
port=3306,
user='root',
passwd="1234",# new databases2 to save title
db="test"
)
#to get all data num
cur = conn.cursor()
sql = "select * from ...."
aa = cur.execute(sql)#check information
num = aa.fetchmany(aa)
for i in range(0,num):
# part one to get content
cur = conn.cursor()
sql = "select * from ...."
aa = cur.execute(sql) # check information
print aa
# to get data num
num = aa.fetchmany(aa)
# part two to post content
cur2 = conn.cursor()
sql2 = "insert into ... values(%s,%s,%s,%s)"
cur2.execute(sql2, ('1', '2', '3', '4'))
# part three to post title
cur3 = conn.cursor()
sql3 = "insert into ... values(%s,%s,%s,%s)"
cur3.execute(sql3, ('1', '2', '3', '4'))
cur.close()
conn.commit()
conn.close()
cur2.close()
conn2.commit()
conn2.close()
cur3.close()
conn3.commit()
conn3.close()