Sqlite3 实现学生信息增删改查
2024-09-03 16:02:18
import sqlite3
conn = sqlite3.connect('studentsdb.db')
# 连接数据库
cursor = conn.cursor( )
# 创建数据表 def createDatabase():
'''创建一个数据表'''
sql = 'create table student(stuId int primary key,stuName text,stuAge text,stuGender text,stuClass text)'
cursor.execute(sql)
conn.commit() def addInfo(sql = ''):
'''添加数据'''
if sql =='':
# 如果是初始化,则默认会进行增加 6 条数据
stuInfo = [(1001, '小华', '', '男', '二班'),
(1002, '小明', '', '女', '二班'),
(1003, '小李', '', '女', '一班'),
(1004, '小王', '', '男', '一班'),
(1005, '小刘', '', '女', '二班'),
(1006, '小张', '', '女', '一班')]
cursor.executemany("insert into student values(?,?,?,?,?)",stuInfo)
# 插入多条语句
conn.commit() def deleteInfo():
'''删除数据'''
cursor.execute("delete from student where stuId = 1005")
# 将学号为 1005 的小刘同学删除
conn.commit() def modifyInfo():
'''修改数据'''
sql = "update student set stuAge = ? where stuId = ?"
cursor.execute(sql,(20,1006))
# 将小张的年龄修改为 20
conn.commit() def selectInfo():
'''查询学生信息'''
sql = 'select * from student'
# 查询全部数据
cursor.execute(sql)
print(cursor.fetchall()) def main():
# 创建一个数据表
createDatabase()
# 添加数据
print("添加六条学生数据之后")
addInfo()
selectInfo()
# 修改数据
print("将小张的年龄修改为 20")
modifyInfo()
selectInfo()
# 删除数据
print("将学号为 1005 的小刘同学删除")
deleteInfo()
selectInfo() # cursor.execute('drop table student')
# conn.commit()
main()
2020-05-24
最新文章
- WebService初学
- wex5 实战 登陆帐号更换与用户id一致性
- findViewById返回null
- Android的学习——ubuntu下android5.1源码的make编译
- Oracle插入时间
- 基于事件的 NIO 多线程服务器--转载
- 修改vim默认tab为4个空格与显示行号
- JFrame
- 【翻译】CSS Animations VS the Web Animations API:案例学习
- EntityFrameWork连接多Db配置
- 粒子滤波(PF:Particle Filter)
- 1.6W star 的 JCSprout 阅读体验大提升
- java整数溢出问题及提升为long型
- 开源前端脚本错误监控及跟踪解决项目-BadJS 试用
- python实现发送邮件
- lis最长上升子序列
- UI自动化selenium
- VMware Workstation 11 搭建windows server 2012 之sql server 2012集群常见问题整理
- tcp连接状态查看
- R dataframe 列名包含减号,列名自动变为点