python读取连接数据库文件_python 连接MySQL数据库 从文件中读取数据进行分块自动插入数据...

automation_insert_into.py(自动插入数据)

conn_mysql(host,port,user,passwd,db)

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

cut_data(file_path,cut_size=5)

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

insert_cut_data(conn,sql,cut_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('数据导入成功!!!')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值