1. import sqlite3
  2.  
  3. conn = sqlite3.connect('E:\\student.db')
  4. print("Opened database successfully")
  5. c = conn.cursor()
  6. c.execute('''CREATE TABLE if not exists STUDENT
  7. (ID INT PRIMARY KEY NOT NULL,
  8. STU_NAME CHAR(20),
  9. AGE INT NOT NULL,
  10. ADDRESS CHAR(50));''')
  11.  
  12. print ("Table created successfully")
  13. conn.commit()
  1. Opened database successfully
  2. Table created successfully
  1. print("Opened database successfully")
  2. c.execute("INSERT INTO STUDENT(ID,STU_NAME,AGE,ADDRESS) VALUES(20154071115,'dch',22,'1-223')")
  3. c.execute("INSERT INTO STUDENT(ID,STU_NAME,AGE,ADDRESS) VALUES(20154071112,'fwx',22,'1-222') ")
  4. c.execute("INSERT INTO STUDENT(ID,STU_NAME,AGE,ADDRESS) VALUES(20154071111,'cg',22,'1-223') ")
  5. c.execute("INSERT INTO STUDENT(ID,STU_NAME,AGE,ADDRESS) VALUES(20154071110,'wyf',22,'1-222') ")
  6. conn.commit()
  7. print("Records created successfully")
  1. Opened database successfully
  2. Records created successfully
  1. cursor = c.execute("SELECT id,STU_NAME,address FROM student")
  2. for row in cursor:
  3. print("ID = ",row[0])
  4. print("NAME = ",row[1])
  5. print("ADDRESS = ",row[2])
  6. print("Operation done successfully")
  7. #conn.close()
  8. def display_menu():
  9. print("学生表操作界面")
  10. print("---------------------")
  11. print("1.增添学生信息")
  12. print("2.查询学生有关资料")
  13. print("3.修改学生有关信息")
  14. print("4.删除学生信息")
  15. print("5.查询现在的学生信息")
  16. print("0.退出")
  17. print("---------------------")
  1. ID = 20154071115
  2. NAME = dch
  3. ADDRESS = 1-223
  4. ID = 20154071112
  5. NAME = fwx
  6. ADDRESS = 1-222
  7. ID = 20154071111
  8. NAME = cg
  9. ADDRESS = 1-223
  10. ID = 20154071110
  11. NAME = wyf
  12. ADDRESS = 1-222
  13. Operation done successfully
  1. def append_data():
  2. id = int(input("请输入新学生的学号:"))
  3. name = str(input("请输入新学生的名字"))
  4. age = int(input("请输入新学生的年龄"))
  5. address = str(input("请输入新学生的地址"))
  6. sqlStr = "select * from student where id = {};".format(id)
  7. cursor = conn.execute(sqlStr)
  8. if len(cursor.fetchall())>0:
  9. print("列表中已经有这个学生了")
  10. else:
  11. sqlStr = "insert into student(ID,STU_NAME,AGE,ADDRESS) VALUES ({},'{}',{},'{}')".format(id,name,age,address)
  12. conn.execute(sqlStr)
  13. conn.commit()
  1. def update_date():
  2. id = int(input("请输入你要修改的学号:"))
  3. sqlStr = "select * from student where id = {};".format(id)
  4. cursor = conn.execute(sqlStr)
  5. rows = cursor.fetchall()
  6. if len(rows) > 0:
  7. print("该学生的姓名是",rows[0][1])
  8. name = input("请输入学生的新名字")
  9. age = int(input("请输入学生的新年龄"))
  10. address = input("请输入学生的新地址")
  11. sqlStr = "update student set STU_NAME = '{}',age = '{}',address = '{}' where id = {}".format(name,age,address,id)
  12. conn.execute(sqlStr)
  13. conn.commit()
  14. print("修改成功")
  15. else:
  16. print("不存在该学生")
  1. def delete_data():
  2. id = int(input("请输入要删除的学生id:"))
  3. sqlStr = "select * from student where id = {};".format(id)
  4. cursor = conn.execute(sqlStr)
  5. rows = cursor.fetchall()
  6. if len(rows) > 0:
  7. print("该学生的姓名是",rows[0][1])
  8. s = int(input("请确认删除(如果删除请输入'1',不删除请输入'0'):"))
  9. if s == 1:
  10. sqlStr = "delete from student where id = {}".format(id)
  11. conn.execute(sqlStr)
  12. print("删除成功")
  13. else:
  14. return display_menu()
  1. def select_data():
  2. id = int(input("请输入要修改的学生id:"))
  3. sqlStr = "select * from student where id = {};".format(id)
  4. cursor = conn.execute(sqlStr)
  5. rows = cursor.fetchall()
  6. if len(rows) > 0:
  7. print("该学生信息如下:")
  8. print(rows)
  9. else:
  10. print("该学生不存在")
  1. def display_data():
  2. cursor = conn.execute('SELECT * FROM student;')
  3. for row in cursor:
  4. print(row)
  5. while True:
  6. display_menu()
  7. choice = int(input("请输入你的选择"))
  8. if choice == 0:
  9. conn.close()
  10. break
  11. elif choice == 2:
  12. select_data()
  13. elif choice == 3:
  14. update_date()
  15. elif choice == 4:
  16. delete_data()
  17. elif choice == 5:
  18. display_data()
  19. elif choice == 1:
  20. append_data()
  21. else:break

吴裕雄--python学习笔记:通过sqlite3 进行文字界面学生管理的更多相关文章

  1. 吴裕雄--python学习笔记:sqlite3 模块

    1 sqlite3.connect(database [,timeout ,other optional arguments]) 该 API 打开一个到 SQLite 数据库文件 database 的 ...

  2. 吴裕雄--python学习笔记:sqlite3 模块的使用与学生信息管理系统

    import sqlite3 cx = sqlite3.connect('E:\\student3.db') cx.execute( '''CREATE TABLE StudentTable( ID ...

  3. 吴裕雄--python学习笔记:爬虫基础

    一.什么是爬虫 爬虫:一段自动抓取互联网信息的程序,从互联网上抓取对于我们有价值的信息. 二.Python爬虫架构 Python 爬虫架构主要由五个部分组成,分别是调度器.URL管理器.网页下载器.网 ...

  4. 吴裕雄--python学习笔记:爬虫包的更换

    python 3.x报错:No module named 'cookielib'或No module named 'urllib2' 1. ModuleNotFoundError: No module ...

  5. 吴裕雄--python学习笔记:爬虫

    import chardet import urllib.request page = urllib.request.urlopen('http://photo.sina.com.cn/') #打开网 ...

  6. 吴裕雄--python学习笔记:os模块函数

    os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix'. os.getcwd:得 ...

  7. 吴裕雄--python学习笔记:os模块的使用

    在自动化测试中,经常需要查找操作文件,比如说查找配置文件(从而读取配置文件的信息),查找测试报告(从而发送测试报告邮件),经常要对大量文件和大量路径进行操作,这就依赖于os模块. 1.当前路径及路径下 ...

  8. 吴裕雄--python学习笔记:BeautifulSoup模块

    import re import requests from bs4 import BeautifulSoup req_obj = requests.get('https://www.baidu.co ...

  9. Python学习笔记(十二)—Python3中pip包管理工具的安装【转】

    本文转载自:https://blog.csdn.net/sinat_14849739/article/details/79101529 版权声明:本文为博主原创文章,未经博主允许不得转载. https ...

随机推荐

  1. 题解 洛谷P2158 【[SDOI2008]仪仗队】

    本文搬自本人洛谷博客 题目 本文进行了一定的更新 优化了 Markdown 中 Latex 语句的运用,加强了可读性 补充了"我们仍不曾知晓得 消失的 性质5 ",加强了推导的严谨 ...

  2. 【MySQL参数】-innodb_additional_mem_pool_size

    原博客:https://yq.aliyun.com/articles/32384

  3. PAT Advanced 1154 Vertex Coloring (25) [set,hash]

    题目 A proper vertex coloring is a labeling of the graph's vertices with colors such that no two verti ...

  4. 注册登录页面修订-Python使用redis-手机验证接口-发送短信验证

    登录页面修订 views.Login.vue <template> <div class="login box"> <img src="@/ ...

  5. Photoshop和Halcon如何锐化彩色图像不伤其颜色

    锐化图像是摄影中的一步重要操作. 锐化是通过颜色提纯达到锐化的目的.一旦锐化过度,照片很容易就会出现不自然的色斑,或溢色效果. 我们以Photoshop中的“USM锐化滤镜”为例:(为了使效果明显,我 ...

  6. 题解【DP100题1~10】

    哎~这事做晚了~ (Dp100计划T1) 只有蓝题及以上才会水题解 分行Dp,行间没有转移 \[ F[L][R] = max(F[L+1][R]+2^k \times V[L],F[L][R-1]+2 ...

  7. LeetCode——735.行星碰撞

    给定一个整数数组 asteroids,表示在同一行的行星. 对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动).每一颗行星以相同的速度移动. 找出 ...

  8. JAVA8 函数式接口

    一.什么是函数式接口 1.只包含一个抽象方法的接口,称为函数式接口. 2.你可以通过Lambda表达式来创建该接口的对象.(若Lambda表达式抛出一个受检异常,那么该异常需要在目标接口的抽象方法上进 ...

  9. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:多线程队列操作

    import tensorflow as tf #1. 定义队列及其操作. queue = tf.FIFOQueue(100,"float") enqueue_op = queue ...

  10. 高精度处理斐波那契序列(C语言)

    #include<stdio.h> #include<string.h> //memset,strcpy,strlen函数头文件 int main(void) { ];//用来 ...