mysql 6.2使用_(六)6-3Mysql操作据二

#!/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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值