(六)6-3Mysql操作据二
生活随笔
收集整理的這篇文章主要介紹了
(六)6-3Mysql操作据二
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
設計表
通過設計經典的學生管理系統,熟悉數據庫的使用。以下是根據課程,成績,老師這幾者的關系設計表結構。
學生表:
| Student | ? | ? | ? | ? |
| 字段名 | 類型 | 是否為空 | 主鍵 | 描述 |
| stdid | int | 否 | 是 | 學生ID |
| stdname | varchar(100) | 否 |
| 學生姓名 |
| gender | enum('M', 'F') | 是 |
| 性別 |
| age | int | 是 |
| 年齡 |
課程表:
| Course | ? | ? | ? | ? |
| 字段名 | 類型 | 是否為空 | 主鍵 | 描述 |
| couid | int | 否 | 是 | 課程ID |
| cname | varchar(50) | 否 |
| 課程名字 |
| tid | int | 否 |
| 老師ID |
成績表
| 字段名 | 類型 | 是否為空 | 主鍵 | 描述 |
| sid | int | 否 | 是 | 分數ID |
| stdid | int | 否 |
| 學生id |
| couid | int | 否 |
| 課程id |
| grade | int | 否 |
| 分數 |
教師表:
| Teacher | ? | ? | ? | ? |
| 字段名 | 類型 | 是否為空 | 主鍵 | 描述 |
| tid | int | 否 | 是 | 老師ID |
| tname | varcher(100) | 否 |
| 老師名字 |
?
?有了表結構,創建表
執行結果:
<_mysql.connection open to '172.16.61.158' at 27b6e48> create table student(stdid int not NULL ,stdname varchar(100) not null,gender enum('M','F'),age int); create table course(couid int not null,cname varchar(50) not null ,tid int not null ); create table score(sid int not null,stdid int not null,couid int not null,grade int not null); create table teacher(tid int not null,tname varchar(100) not null );?有了表,下步添加數據:
?
#!/usr/bin/env python #coding:utf8import MySQLdb def connect_mysql():db_config = {"host":"172.16.61.158","port":3306,"user":"root","passwd":"123456","db":"stu","charset":"utf8",}try:cnx = MySQLdb.connect(**db_config)except Exception as e :raise ereturn cnxstudent = '''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 :print studentcus_students = cnx.cursor()cus_students.execute(student)cus_students.close()print coursecus_course = cnx.cursor()cus_course.execute(course)cus_course.close()print scorecus_score = cnx.cursor()cus_score.execute(score)cus_score.close()print teachercus_theacher = cnx.cursor()cus_theacher.execute(teacher)cus_theacher.close()print("OK")cnx.commit()except Exception as e :cnx.rollback()print('error')raise efinally:cnx.close()運行結果:
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;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;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;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;OK?驗證:
查詢數據
?
#!/usr/bin/env python #coding:utf8import MySQLdb import codecs def connect_mysql():db_config = {"host":"172.16.61.158","port":3306,"user":"root","passwd":"123456","db":"stu","charset":"utf8",}try:cnx = MySQLdb.connect(**db_config)except Exception as e :raise ereturn cnxif __name__ == "__main__":cnx = connect_mysql()sql = '''select * from student where stdname in (select stdname from student group by stdname having count(1)>1 ) order by stdname;'''print sqltry:cus = cnx.cursor()cus.execute(sql)result = cus.fetchall()for res in result:print rescus.close()cnx.commit()except Exception as e:cnx.rollback()print('error')raise efinally:cnx.close()運行結果:
select * from student where stdname in (select stdname from student group by stdname having count(1)>1 ) order by stdname; (19001L, u'e03fd17e980627e67c2e1b583f611e4a0855e46e176237ea5e6ba7c2c6a992787447ad57d0a4597d', u'F', 21L) (19021L, u'e03fd17e980627e67c2e1b583f611e4a0855e46e176237ea5e6ba7c2c6a992787447ad57d0a4597d', u'M', 25L) (19028L, u'e03fd17e980627e67c2e1b583f611e4a0855e46e176237ea5e6ba7c2c6a992787447ad57d0a4597d', u'F', 23L)?
轉載于:https://www.cnblogs.com/pythonlx/p/7881152.html
總結
以上是生活随笔為你收集整理的(六)6-3Mysql操作据二的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 车上km&#47;h踩油门才显示
- 下一篇: 启辰t70加油门有异响?