吴裕雄--python学习笔记:通过sqlite3 进行文字界面学生管理
import sqlite3
conn = sqlite3.connect('E:\\student.db')
print("Opened database successfully")
c = conn.cursor()
c.execute('''CREATE TABLE if not exists STUDENT
(ID INT PRIMARY KEY NOT NULL,
STU_NAME CHAR(20),
AGE INT NOT NULL,
ADDRESS CHAR(50));''')
print ("Table created successfully")
conn.commit()
Opened database successfully
Table created successfully
print("Opened database successfully")
c.execute("INSERT INTO STUDENT(ID,STU_NAME,AGE,ADDRESS) VALUES(20154071115,'dch',22,'1-223')")
c.execute("INSERT INTO STUDENT(ID,STU_NAME,AGE,ADDRESS) VALUES(20154071112,'fwx',22,'1-222') ")
c.execute("INSERT INTO STUDENT(ID,STU_NAME,AGE,ADDRESS) VALUES(20154071111,'cg',22,'1-223') ")
c.execute("INSERT INTO STUDENT(ID,STU_NAME,AGE,ADDRESS) VALUES(20154071110,'wyf',22,'1-222') ")
conn.commit()
print("Records created successfully")
Opened database successfully
Records created successfully
cursor = c.execute("SELECT id,STU_NAME,address FROM student")
for row in cursor:
print("ID = ",row[0])
print("NAME = ",row[1])
print("ADDRESS = ",row[2])
print("Operation done successfully")
#conn.close()
def display_menu():
print("学生表操作界面")
print("---------------------")
print("1.增添学生信息")
print("2.查询学生有关资料")
print("3.修改学生有关信息")
print("4.删除学生信息")
print("5.查询现在的学生信息")
print("0.退出")
print("---------------------")
ID = 20154071115
NAME = dch
ADDRESS = 1-223
ID = 20154071112
NAME = fwx
ADDRESS = 1-222
ID = 20154071111
NAME = cg
ADDRESS = 1-223
ID = 20154071110
NAME = wyf
ADDRESS = 1-222
Operation done successfully
def append_data():
id = int(input("请输入新学生的学号:"))
name = str(input("请输入新学生的名字"))
age = int(input("请输入新学生的年龄"))
address = str(input("请输入新学生的地址"))
sqlStr = "select * from student where id = {};".format(id)
cursor = conn.execute(sqlStr)
if len(cursor.fetchall())>0:
print("列表中已经有这个学生了")
else:
sqlStr = "insert into student(ID,STU_NAME,AGE,ADDRESS) VALUES ({},'{}',{},'{}')".format(id,name,age,address)
conn.execute(sqlStr)
conn.commit()
def update_date():
id = int(input("请输入你要修改的学号:"))
sqlStr = "select * from student where id = {};".format(id)
cursor = conn.execute(sqlStr)
rows = cursor.fetchall()
if len(rows) > 0:
print("该学生的姓名是",rows[0][1])
name = input("请输入学生的新名字")
age = int(input("请输入学生的新年龄"))
address = input("请输入学生的新地址")
sqlStr = "update student set STU_NAME = '{}',age = '{}',address = '{}' where id = {}".format(name,age,address,id)
conn.execute(sqlStr)
conn.commit()
print("修改成功")
else:
print("不存在该学生")
def delete_data():
id = int(input("请输入要删除的学生id:"))
sqlStr = "select * from student where id = {};".format(id)
cursor = conn.execute(sqlStr)
rows = cursor.fetchall()
if len(rows) > 0:
print("该学生的姓名是",rows[0][1])
s = int(input("请确认删除(如果删除请输入'1',不删除请输入'0'):"))
if s == 1:
sqlStr = "delete from student where id = {}".format(id)
conn.execute(sqlStr)
print("删除成功")
else:
return display_menu()
def select_data():
id = int(input("请输入要修改的学生id:"))
sqlStr = "select * from student where id = {};".format(id)
cursor = conn.execute(sqlStr)
rows = cursor.fetchall()
if len(rows) > 0:
print("该学生信息如下:")
print(rows)
else:
print("该学生不存在")
def display_data():
cursor = conn.execute('SELECT * FROM student;')
for row in cursor:
print(row)
while True:
display_menu()
choice = int(input("请输入你的选择"))
if choice == 0:
conn.close()
break
elif choice == 2:
select_data()
elif choice == 3:
update_date()
elif choice == 4:
delete_data()
elif choice == 5:
display_data()
elif choice == 1:
append_data()
else:break
吴裕雄--python学习笔记:通过sqlite3 进行文字界面学生管理的更多相关文章
- 吴裕雄--python学习笔记:sqlite3 模块
1 sqlite3.connect(database [,timeout ,other optional arguments]) 该 API 打开一个到 SQLite 数据库文件 database 的 ...
- 吴裕雄--python学习笔记:sqlite3 模块的使用与学生信息管理系统
import sqlite3 cx = sqlite3.connect('E:\\student3.db') cx.execute( '''CREATE TABLE StudentTable( ID ...
- 吴裕雄--python学习笔记:爬虫基础
一.什么是爬虫 爬虫:一段自动抓取互联网信息的程序,从互联网上抓取对于我们有价值的信息. 二.Python爬虫架构 Python 爬虫架构主要由五个部分组成,分别是调度器.URL管理器.网页下载器.网 ...
- 吴裕雄--python学习笔记:爬虫包的更换
python 3.x报错:No module named 'cookielib'或No module named 'urllib2' 1. ModuleNotFoundError: No module ...
- 吴裕雄--python学习笔记:爬虫
import chardet import urllib.request page = urllib.request.urlopen('http://photo.sina.com.cn/') #打开网 ...
- 吴裕雄--python学习笔记:os模块函数
os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix'. os.getcwd:得 ...
- 吴裕雄--python学习笔记:os模块的使用
在自动化测试中,经常需要查找操作文件,比如说查找配置文件(从而读取配置文件的信息),查找测试报告(从而发送测试报告邮件),经常要对大量文件和大量路径进行操作,这就依赖于os模块. 1.当前路径及路径下 ...
- 吴裕雄--python学习笔记:BeautifulSoup模块
import re import requests from bs4 import BeautifulSoup req_obj = requests.get('https://www.baidu.co ...
- Python学习笔记(十二)—Python3中pip包管理工具的安装【转】
本文转载自:https://blog.csdn.net/sinat_14849739/article/details/79101529 版权声明:本文为博主原创文章,未经博主允许不得转载. https ...
随机推荐
- 邮件发送,无尽的501错误。TCP发送邮件解决方案
先贴上错误信息,便于搜索引擎采集,也送给遇到此问题的技术朋友们. smtp 501 Syntax error (no parameters allowed) (#5.5.4) 背景描述: 使用TCP发 ...
- one_day_one_linuxCmd---wget命令
<坚持每天学习一个 linux 命令,今天我们来学习 wget 命令> Linux wget 是一个文件下载工具,它用在命令行下,使用也非常方便,不但体积小而且功能完善,并支持很多强大的功 ...
- win32概述
win32基于已有的框架 有意入口函数只有一个 都需要有一个主函数 所有程序的入口都是maincrtstartup tydedef 顾名思义 window是基于c,c++ 又想有自己所特有的数据类型 ...
- UI Automation技术获取cmd或Powershell命令提示符窗口的实时内容
事先打开的Powershell或cmd窗口中的文本,用其他方式难以拿到.但是用UI Automation可以轻松获取.本工具在窗体上加入了一个Timer控件,每秒钟都查找桌面上是否有Powershel ...
- Python常用模块小结
目录 Python常用模块小结 一.Python常用模块小结 1.1 time模块 1.2 datetime模块 1.3 random模块 1.4 os模块 1.5 sys模块 1.6 json模块 ...
- Centos7安装Xrdp远程桌面
Xrdp是Microsoft远程桌面协议RDP的一个开源实现,它允许以图像方式控制远程系统. 测试环境 服务端: CentOS Linux release 7.7.1908 (Core) 客户端: W ...
- LeetCode——199. 二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 < ...
- Java——Collection集合、迭代器、泛型
集合 ——集合就是java提供的一种容器,可以用来存储多个数据. 集合和数组的区别 数组的长度是固定的.集合的长度是可变的. 数组中存储的是同一类型的元素,可以存储基本数据类型值. 集合存储的都是对象 ...
- eclipse优化自动补全(解决空格、分号等上屏问题)
第一步:打开"Window"——>"Show View"——>"Other"——>找到"Plug-in Deve ...
- 894A. QAQ#(暴力)
题目出处:http://codeforces.com/problemset/problem/894/A 题目大意:计数依次出现QAQ的次数 #include<iostream> using ...