MYSQL转SQL Server脚本

日常工作中,经常会碰到数据库间的导入导出,如果碰到数据量小的处理起来不是那么棘手,网上也有很多mysql转成sqlserver的工具,可是碰到数据较大的(例如百万甚至以上),利用工具大多时候都比较失败,学习python后,处理起来就方便多了。下面是一个小脚本:

# -*- coding:UTF-8 -*-


""" mysql脚本导入到sqlserver中去 author: zhangbc create_time: 2017-05-04 """

import sys
import time
import pyodbc

reload(sys)
sys.setdefaultencoding('UTF-8')


def read_sql(filename='inkks_users.sql', res_filename='hj_users.sql'):
    """  转换mysql的sql文件,符合sql server格式  :param fliename: 处理前文件名  :param res_filename: 处理后文件名  :return:  """

    with open(filename, 'r') as fp:
        content = fp.read().replace('`', '').replace(r'\\', '').replace(r'\'', '')
        open(res_filename, 'w').write(content)


def exec_sql(filename='hj_users.sql'):
    """  执行SQL SERVER文件  :param filename: sqlserver文件名  :return:  """

    try:
        conn = pyodbc.connect(DRIVER='{SQL Server}',
                              SERVER='127.0.0.1',
                              DATABASE='xxxxxxxxx',
                              UID='xxx',
                              PWD='xxxxxxxxxx',
                              charset='utf-8')
        cur = conn.cursor()
    except pyodbc.DatabaseError, ex:
        print 'SQL Server connecting error,reason is:'+str(ex)
        sys.exit()

    counts = 0
    with open(filename, 'r') as fp:
        while True:
            line = fp.readline()
            if not line:
                print 'the items of %d is finished! -- %s ' % \
                      (counts, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
                break

            try:
                cur.execute(u'%s' % line)
                conn.commit()
                counts += 1
            except pyodbc.Error:
                open('res_error.sql', 'a').write(line)

            if counts % 100000 == 0:
                print 'the items of %d is finished! -- %s ' % \
                      (counts, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))

    cur.close()
    conn.close()


def main():
    """  主函数  :return:  """

    # 转换sql脚本
    read_sql('res_error.sql', '11.sql')

    # 执行SQL脚本
    # exec_sql()


if __name__ == '__main__':

    start_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    print 'Beginning ! Begin:' + start_time
    main()
    end_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    print '\nEnd:' + end_time

也许你会利用sqlcmd导入sql脚本,但是从mysql导出的脚本还是会出现问题,例如mysql表出现“\’”,”\\“等字段值。

    原文作者:张伯成
    原文地址: https://zhuanlan.zhihu.com/p/26712517
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞