#!/usr/bin/env python#coding:utf8
importMySQLdbdefconnect_mysql():
db_config={"host":"172.16.61.158","port":3306,"user":"root","passwd":"123456","db":"stu","charset":"utf8",
}try:
cnx= MySQLdb.connect(**db_config)exceptException as e :raiseereturncnx
student= '''set @i:= 10000;
insert into student select @i:=@i +1,substr(concat(sha1(rand()),sha1(rand())),1,5+floor(rand() +100)),case floor(rand()*10)mod 2 when 1 then 'M' else 'F' end ,25-floor(rand()*5) from tmp a,tmp b,tmp c,tmp d;'''course= '''set @i := 10;
insert into course select @i:=@i+1, substr(concat(sha1(rand()), sha1(rand())), 1, 5 + floor(rand() * 40)), 1 + floor(rand() * 100) from tmp a;'''score= '''set @i := 10000;
insert into score select @i := @i +1, floor(10001 + rand()*10000), floor(11 + rand()*10), floor(1+rand()*100) from tmp a, tmp b, tmp c, tmp d;'''teacher= '''set @i := 100;
insert into teacher select @i:=@i+1, substr(concat(sha1(rand()), sha1(rand())), 1, 5 + floor(rand() * 80)) from tmp a, tmp b;'''
if __name__ == "__main__":
cnx=connect_mysql()try:printstudent
cus_students=cnx.cursor()
cus_students.execute(student)
cus_students.close()printcourse
cus_course=cnx.cursor()
cus_course.execute(course)
cus_course.close()printscore
cus_score=cnx.cursor()
cus_score.execute(score)
cus_score.close()printteacher
cus_theacher=cnx.cursor()
cus_theacher.execute(teacher)
cus_theacher.close()print("OK")
cnx.commit()exceptException as e :
cnx.rollback()print('error')raiseefinally:
cnx.close()