mysql 批量导入脚本

mysqldump 单表备份,py还原

调整下允许的包大小

max_allowed_packet = 10M

#!/usr/local/bin/python3
# coding:utf-8

# ====================================================
# Author: chang - EMail:changbo@hmg100.com
# Last modified: 2017-4-28
# Filename: importmysql.py
# Description: import sql,base os,pymysql
# blog:http://www.cnblogs.com/changbo
# ====================================================

import os
import pymysql

filelist = []
listfile = os.listdir('/xxxx/xxxx')
for line in listfile:
    filelist.append(line)

db = pymysql.connect('x.x.x.x', 'xxx', 'xxxxx', 'xxxxx')
db.set_charset('utf8')

cursor = db.cursor()


i = 0
for i in range(len(filelist)):
    try:
       commond = open('/xxxx/xxxx/%s' % filelist[i]).read()
       cursor.execute(commond)
       db.commit()
       results = cursor.fetchall()
       print(filelist[i])
    except Exception as e:
       print(e)
       pass

db.close()    

END!

posted @ 2017-04-28 14:02  知_行  阅读(193)  评论(0编辑  收藏  举报