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

最新文章

  1. WebService初学
  2. wex5 实战 登陆帐号更换与用户id一致性
  3. findViewById返回null
  4. Android的学习——ubuntu下android5.1源码的make编译
  5. Oracle插入时间
  6. 基于事件的 NIO 多线程服务器--转载
  7. 修改vim默认tab为4个空格与显示行号
  8. JFrame
  9. 【翻译】CSS Animations VS the Web Animations API:案例学习
  10. EntityFrameWork连接多Db配置
  11. 粒子滤波(PF:Particle Filter)
  12. 1.6W star 的 JCSprout 阅读体验大提升
  13. java整数溢出问题及提升为long型
  14. 开源前端脚本错误监控及跟踪解决项目-BadJS 试用
  15. python实现发送邮件
  16. lis最长上升子序列
  17. UI自动化selenium
  18. VMware Workstation 11 搭建windows server 2012 之sql server 2012集群常见问题整理
  19. tcp连接状态查看
  20. R dataframe 列名包含减号,列名自动变为点

热门文章

  1. 阿里云centos7安装jdk8
  2. Windows常用注册表文件
  3. Wooden Stricks——两个递增条件的线性DP
  4. 2020阿里巴巴官方最新Redis开发规范!
  5. 一、web自动化快速使用
  6. 常用API - 包装类、System类
  7. 基础设计模式-02 UML图
  8. 1.Unity3d的新建场景和保存场景
  9. python入门008
  10. 攻防世界-Web-ics-05