1 #!/usr/bin/python3
2 #coding=utf-8
3 """
4 ***********************help document****************************************5 Usage:6 this is a simple student grades system,now is simple7 when start the programming,you can do following operations8 enter ‘a‘ is to insert data into database9 enter ‘b‘ is to display all data in database10 enter ‘c‘ is to query the specify info in database11 enter ‘h‘ is to see this help dacument12 enter ‘d‘ is to delete someone student‘s info13 enter nothing is to do nothing,you can enter again14 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 importpymysql23
24 #打印帮助文档
25 defhelp_document():26 print(__doc__)27
28 #在已有的数据库上创建数据库的表
29 defcreate_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 definsert_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 defdisplay_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 inresult: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 defquery_db(name):82 db = pymysql.connect(‘localhost‘, ‘root‘, ‘root‘, ‘student_db‘)83 cursor =db.cursor()84 sql = "select * from student_table where name = ‘%s‘" %name85 try:86 cursor.execute(sql)87 result =cursor.fetchall()88 for row inresult: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 defupdate_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 defdelete_db(name):117 db = pymysql.connect(‘localhost‘, ‘root‘, ‘root‘, ‘student_db‘)118 cursor =db.cursor()119 sql = "delete from student_table where name = ‘%s‘" %name120 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 classswitch(object):130 def __init__(self, value):131 self.value =value132 self.fall =False133
134 def __iter__(self):135 """Return the match method once, then stop"""
136 yieldself.match137 raiseStopIteration138
139 def match(self, *args):140 """Indicate whether or not to enter a case suite"""
141 if self.fall or notargs:142 returnTrue143 elif self.value in args: #changed for v1.5, see below
144 self.fall =True145 returnTrue146 else:147 returnFalse148
149 #建立一个学生类
150 classstudent:151 #构造函数
152 def __init__(self, name, age, id, grade):153 self.next =None154 self.name =name155 self.age =age156 self.id =id157 self.grade =grade158 #每个学生可以输出自己的信息
159 defshow(self):160 print(‘name:‘, self.name, ‘ ‘, ‘age:‘, self.age, ‘ ‘, ‘id:‘, self.id, ‘ ‘, ‘grade:‘, self.grade)161
162 #建立一个学生列表类
163 classstulist:164 #构造函数
165 def __init__(self):166 self.head = student(‘‘, 0, 0, 0)167 #输出数据库中所有的数据
168 defdisplay(self):169 display_db()170 #新增学生数据
171 definsert(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 defquery(self):181 name = input(‘please enter the name you want to query:‘)182 query_db(name)183
184 #删除学生数据
185 defdelete(self):186 name = input("please enter the student‘name you want to delete:")187 delete_db(name)188
189 #主函数,程序的入口
190 defmain():191 stulist1 =stulist()192 user_input = input(‘please enter the OPcode:‘)193 whileuser_input:194 print("a--insert/b--display/c--query/h--help/d--delete/‘‘--default")195 for case inswitch(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()