python 从文件读写,mysql批量插入测试数据
采用了两种方法,第一中直接向mysql插入随机测试数据,第二种采用读写文件从文件中读入数据插入mysql,适合从生产环境导入数据到测试环境库。
# encoding=utf-8
# 测试数据制造
import pymysql
import random
import time
import string
import csv
import os
conn = pymysql.connect(host='192.168.152.128', port=3306, user='root', passwd='root', db='test')
# 循环生成随机数据到插入到mysql中
# 三个字段,id,name,age
class TEST_INSERT_MYSQL:
def __init__(self, conn):
self.conn = conn
def init_mysql_data(self):
try:
cur = conn.cursor()
cur.execute("drop table if exists student")
sql = "create table if not exists student(id int not null AUTO_INCREMENT PRIMARY KEY ,name char(20),age char(3))" # 自增长
cur.execute(sql)
print(time.ctime())
for i in range(1, 1000):
random_name = ''.join(random.sample(string.ascii_letters + string.digits, 8)) # 8位随机字符
random_age = ''.join(str(random.randint(10, 120))) # 10-120随机字符
value = (random_name, random_age)
insert_sql = "insert into student(name,age) values(%s,%s)"
cur.execute(insert_sql, value)
i += 1
conn.commit()
print(time.ctime())
except:
conn.rollback()
print("Insert fail")
cur.close()
conn.close()
#定义了一个写入函数,从文件读取数据插入数据库
def insert_mysql(self, data):
cur = conn.cursor()
print(time.ctime())
for i in range(1, len(data)):
insert_sql = "insert into student(name,age) values(%s,%s)"
value = (data[i][1], data[i][2])
cur.execute(insert_sql, value)
i += 1
conn.commit()
print(time.ctime())
cur.close()
conn.close()
# 读写操作txt文本文件,定义一个写函数一个读函数
class OPREATE_FILE():
# 写测试文件
def write_file(self, filename):
if os.path.exists(filename): # 删除存在的测试文件
os.remove(filename)
with open(filename, 'w') as f:
for i in range(1, 100):
random_name = ''.join(random.sample(string.ascii_letters + string.digits, 8))
random_age = str(random.randint(10, 120))
list = [str(i)]
list.append(random_name)
list.append(random_age)
data = ','.join(list)
f.write(data + '\n')
assert isinstance(i, object)
i += i
# 读测试文件
def read_file(self, filename):
list = []
with open(filename, 'r') as f:
for lines in f:
line = lines.split(',')
data = (line[0], line[1], line[2].replace("\n", ""))
list.append(data)
return list
#以下为测试部分
Test = TEST_INSERT_MYSQL(conn)
# Test.insert_mysql_data()
Test_data = OPREATE_FILE()
# Test_data.write_file('test_data.txt')
A = Test_data.read_file('test_data.txt')
Test.insert_mysql(A)