思路:
循环指定目录中的每个.sql文件,读取每行内容,拼接到列表中,按每次1000行执行。
备注:
pymysql在8.0版本以前(不包含8.0)默认可以同时执行多条sql语句的,例如“insert into tb(1);insert into tb(2);”
但是在8.0之后不再为默认,需要手动配置pymysql.connect的参数client_flag=CLIENT.MULTI_STATEMENTS,否则会报错。
import os import pymysql from pymysql.constants import CLIENT # pymysql在8.0版本之后需要手动配置,以支持同时执行多条sql语句 conn = pymysql.connect(host='192.168.1.17', user='root', passwd="password", db='db1', client_flag=CLIENT.MULTI_STATEMENTS) cur = conn.cursor() path = r'D:\Projects\python\mysqltool\sql\new' for filename in os.listdir(path): if filename.endswith(".sql") == False: continue count = 0 time = 0 sql = [] filepath = os.path.join(path,filename) with open(filepath, "r", encoding="utf-8") as f: for each_line in f.readlines(): if not each_line or each_line == "\n": continue if count < 1000: sql.append(each_line.strip()) count += 1 else: #达到1000行时执行 time += 1 print('{}第{}次开始执行sql语句,记录数:{}'.format(filename, time, count)) cur.execute("".join(sql)) conn.commit() sql = [each_line.strip()] count = 1 #读取完文件,不到1000行 if sql: time += 1 print('{}第{}次开始执行sql语句,记录数:{}'.format(filename, time, count)) cur.execute("".join(sql)) conn.commit()
标签:count,Python,sql,pymysql,filename,each,line From: https://www.cnblogs.com/gdjlc/p/16866250.html