基于python的学生管理系统(含数据库版本)
这次支持连接到后台的数据库,直接和数据库进行交互,实现基本的增删查改
1 #!/usr/bin/python3 2 # coding=utf-8 3 """ 4 ***********************help document**************************************** 5 Usage: 6 this is a simple student grades system,now is simple 7 when start the programming,you can do following operations 8 enter 'a' is to insert data into database 9 enter 'b' is to display all data in database 10 enter 'c' is to query the specify info in database 11 enter 'h' is to see this help dacument 12 enter 'd' is to delete someone student's info 13 enter nothing is to do nothing,you can enter again 14 simple help document is: "a--insert/b--display/c--query/h--help/d--delete/''--default" 15 Example: 16 please enter the OPcode: a then [enter] 17 ***************************************************************************** 18 19 """ 20 21 #引入pymysql这个库用来连接数据据 22 import pymysql 23 24 #打印帮助文档 25 def help_document(): 26 print(__doc__) 27 28 #在已有的数据库上创建数据库的表 29 def create_database(): 30 #在mysql中创建数据库student_db 31 db = pymysql.connect('localhost', 'root', 'root') 32 name = 'student_db' 33 cursor = db.cursor() 34 cursor.execute('drop database if exists ' + name) 35 cursor.execute('create database if not exists ' + name) 36 db.close() 37 #在数据库student_db中创建数据表student_table 38 db = pymysql.connect('localhost', 'root', 'root', 'student_db') 39 cursor = db.cursor() 40 name = 'student_table' 41 cursor.execute('drop table if exists ' + name) 42 sql = """create table student_table( 43 name varchar(30) primary key not null, 44 age varchar(10), 45 id varchar(20), 46 grade varchar(10))""" 47 cursor.execute(sql) 48 db.commit() 49 db.close() 50 51 #数据库的插入 52 def insert_db(name,age,id,grade): 53 db = pymysql.connect('localhost', 'root', 'root','student_db') 54 cursor = db.cursor() 55 sql = "insert into student_table (name,age,id,grade) values ('%s','%s','%s','%s')" % \ 56 (name,age,id,grade) 57 cursor.execute(sql) 58 db.commit() 59 db.close() 60 61 #打印数据库中所有数据 62 def display_db(): 63 db = pymysql.connect('localhost', 'root', 'root', 'student_db') 64 cursor = db.cursor() 65 sql = "select * from student_table" 66 try: 67 cursor.execute(sql) 68 result = cursor.fetchall() 69 for row in result: 70 name = row[0] 71 age = row[1] 72 id = row[2] 73 grade = row[3] 74 print("name: '%s',age: '%s',id: '%s',grade: '%s'" % (name,age,id,grade)) 75 print("that's all diaplayed!") 76 except: 77 print('nothing has been displayed...') 78 db.close() 79 80 #数据库的查找 81 def query_db(name): 82 db = pymysql.connect('localhost', 'root', 'root', 'student_db') 83 cursor = db.cursor() 84 sql = "select * from student_table where name = '%s' " % name 85 try: 86 cursor.execute(sql) 87 result = cursor.fetchall() 88 for row in result: 89 name1 = row[0] 90 age1 = row[1] 91 id1 = row[2] 92 grade1 = row[3] 93 print("name: '%s',age: '%s',id: '%s',grade: '%s'" % \ 94 (name1,age1,id1,grade1)) 95 print('the query is over!') 96 except: 97 print('can not query data!') 98 db.close() 99 100 #更新数据库 101 def update_db(name,age,id,grade): 102 db = pymysql.connect('localhost', 'root', 'root', 'student_db') 103 cursor = db.cursor() 104 sql = "update student_table set age = '%s',id = '%s',grade = '%s' where name = '%s'" % \ 105 (age,id,grade,name) 106 try: 107 cursor.execute(sql) 108 db.commit() 109 print('updated successfully!') 110 except: 111 print('can not update data!') 112 db.close() 113 114 115 #数据库的删除 116 def delete_db(name): 117 db = pymysql.connect('localhost', 'root', 'root', 'student_db') 118 cursor = db.cursor() 119 sql = "delete from student_table where name = '%s'" % name 120 try: 121 cursor.execute(sql) 122 db.commit() 123 print('delete the student info successfully!') 124 except: 125 print('delete failed...') 126 db.close() 127 128 # 实现switch-case语句 129 class switch(object): 130 def __init__(self, value): 131 self.value = value 132 self.fall = False 133 134 def __iter__(self): 135 """Return the match method once, then stop""" 136 yield self.match 137 raise StopIteration 138 139 def match(self, *args): 140 """Indicate whether or not to enter a case suite""" 141 if self.fall or not args: 142 return True 143 elif self.value in args: # changed for v1.5, see below 144 self.fall = True 145 return True 146 else: 147 return False 148 149 #建立一个学生类 150 class student: 151 #构造函数 152 def __init__(self, name, age, id, grade): 153 self.next = None 154 self.name = name 155 self.age = age 156 self.id = id 157 self.grade = grade 158 #每个学生可以输出自己的信息 159 def show(self): 160 print('name:', self.name, ' ', 'age:', self.age, ' ', 'id:', self.id, ' ', 'grade:', self.grade) 161 162 #建立一个学生列表类 163 class stulist: 164 #构造函数 165 def __init__(self): 166 self.head = student('', 0, 0, 0) 167 #输出数据库中所有的数据 168 def display(self): 169 display_db() 170 #新增学生数据 171 def insert(self): 172 print('please enter:') 173 name = input('name:') 174 age = input('age:') 175 id = input('id:') 176 grade = input('grade:') 177 insert_db(name, age, id, grade) 178 179 #查询学生数据 180 def query(self): 181 name = input('please enter the name you want to query:') 182 query_db(name) 183 184 #删除学生数据 185 def delete(self): 186 name = input("please enter the student'name you want to delete:") 187 delete_db(name) 188 189 #主函数,程序的入口 190 def main(): 191 stulist1 = stulist() 192 user_input = input('please enter the OPcode:') 193 while user_input: 194 print("a--insert/b--display/c--query/h--help/d--delete/''--default") 195 for case in switch(user_input): 196 if case('a'): #按下'a'键 197 stulist1.insert() 198 user_input = input('please enter the OPcode:') 199 break 200 if case('b'): #按下'b'键 201 stulist1.display() 202 user_input = input('please enter the OPcode:') 203 break 204 if case('c'): #按下'c'键 205 stulist1.query() 206 user_input = input('please enter the OPcode:') 207 break 208 if case('d'): #按下'd'键 209 stulist1.delete() 210 user_input = input('please enter your OPcode:') 211 break 212 if case('h'): #按下'h'键 213 help_document() 214 user_input = input('please enter your OPcode:') 215 break 216 if case(): # default 217 print('please enter the OPcode...') 218 user_input = input('please enter the OPcode:') 219 break 220 221 222 if __name__ == "__main__": 223 #第一次运行程序需要建立新的数据库,需要运行下面注释的一行代码,下次运行得将其注释掉 224 #create_database() 225 main()
结果效果图:
输入'h'查看帮助文档
输入'b'来查询数据库数据:
这个版本还是很粗糙,访问速度也很慢,如果以后还有时间的话,我会做一个界面出来,改进下和数据库的交互方式。
少一些功利主义的追求,多一些不为什么的坚持!