def conn_mysql(host='xxxxx',port=3306,user='xxx',passwd='xxxx',db='xxx'):
'''
mysql_ip_path:param host:
mysql_port:param port:
mysql_user:param user:
mysql_passwd:param passwd:
mysql_db:param db:
mysql_conn:return:
'''
conn = pymysql.connect(
host=host,
port=port,
user=user,
passwd=passwd,
db=db
)
return conn
def cut_data(file_path,cut_size=5):
'''
需要导入到数据库的文件:param file_path:
将数据分多少块:param cut_size:
分块后数据:return:
'''
all_data = []
for i in range(cut_size):
all_data.append([])
with open(file_path,'r',encoding='utf-8')as f:
lines = f.readlines()
k = 1
for i in lines:
sub = k % cut_size
data = i.replace('\n', '').split(',')
data.insert(0, str(uuid.uuid1()).replace('-', ''))
all_data[sub].append(tuple(data))
return all_data
def insert_cut_data(conn,sql,cut_data):
'''
mysql_conn:param conn:
MySQL_statement:param sql:
分块后的数据:param cut_data:
None:return:
'''
cur = conn.cursor()
for i in cut_data:
cur.executemany(sql,i)
conn.commit()
cur.close()
conn.close()
print('数据导入成功!!!')
4.automation_insert_into.py
#coding:utf-8
import pymysql
import uuid
'''
__author__ == ForterJ
__date__ == 2019/4/3
'''
def conn_mysql(host='*',port='*',user='*',passwd='*',db='*'):
'''
mysql_ip_path:param host:
mysql_port:param port:
mysql_user:param user:
mysql_passwd:param passwd:
mysql_db:param db:
mysql_conn:return:
'''
conn = pymysql.connect(
host=host,
port=port,
user=user,
passwd=passwd,
db=db
)
return conn
def cut_data(file_path,cut_size=5):
'''
需要导入到数据库的文件:param file_path:
将数据分多少块:param cut_size:
分块后数据:return:
'''
all_data = []
for i in range(cut_size):
all_data.append([])
with open(file_path,'r',encoding='utf-8')as f:
lines = f.readlines()
k = 1
for i in lines:
sub = k % cut_size
data = i.replace('\n', '').split(',')
data.insert(0, str(uuid.uuid1()).replace('-', ''))
all_data[sub].append(tuple(data))
return all_data
def insert_cut_data(conn,sql,cut_data):
'''
mysql_conn:param conn:
MySQL_statement:param sql:
分块后的数据:param cut_data:
None:return:
'''
cur = conn.cursor()
for i in cut_data:
cur.executemany(sql,i)
conn.commit()
cur.close()
conn.close()
print('数据导入成功!!!')