在sqlite数据库中创建一个新的二维表,记录某个班级学生的考试成绩,字段包括:学号、姓名、语文、数学、英语、理综以及总分,设置合适的数据类型,并实现下列操作: (1)使用SQL的insert语句添加至少10位学生的成绩记录; (2)使用fetchall()方法查询数学成绩不及格的学生记录; (3)使用SQL的update语句修改某一位学生的语文成绩; (4)使用SQL的delete语句将数学成绩不及格的学生记录删除。
时间: 2023-06-14 11:03:42 浏览: 82
下面是相应的代码实现:
```python
import sqlite3
# 连接到数据库
conn = sqlite3.connect('class.db')
c = conn.cursor()
# 创建表
c.execute('''CREATE TABLE IF NOT EXISTS scores
(id INTEGER PRIMARY KEY AUTOINCREMENT,
student_id INTEGER,
name TEXT,
chinese INTEGER,
math INTEGER,
english INTEGER,
science INTEGER,
total INTEGER);''')
# 添加学生成绩记录
c.execute("INSERT INTO scores(student_id, name, chinese, math, english, science, total) VALUES (101, '张三', 80, 75, 90, 85, 330)")
c.execute("INSERT INTO scores(student_id, name, chinese, math, english, science, total) VALUES (102, '李四', 85, 60, 70, 80, 295)")
c.execute("INSERT INTO scores(student_id, name, chinese, math, english, science, total) VALUES (103, '王五', 90, 95, 80, 90, 355)")
c.execute("INSERT INTO scores(student_id, name, chinese, math, english, science, total) VALUES (104, '赵六', 70, 65, 75, 75, 285)")
c.execute("INSERT INTO scores(student_id, name, chinese, math, english, science, total) VALUES (105, '钱七', 80, 40, 65, 55, 240)")
c.execute("INSERT INTO scores(student_id, name, chinese, math, english, science, total) VALUES (106, '孙八', 60, 50, 70, 80, 260)")
c.execute("INSERT INTO scores(student_id, name, chinese, math, english, science, total) VALUES (107, '周九', 90, 85, 90, 95, 360)")
c.execute("INSERT INTO scores(student_id, name, chinese, math, english, science, total) VALUES (108, '吴十', 75, 80, 85, 90, 330)")
c.execute("INSERT INTO scores(student_id, name, chinese, math, english, science, total) VALUES (109, '郑十一', 85, 90, 95, 90, 360)")
c.execute("INSERT INTO scores(student_id, name, chinese, math, english, science, total) VALUES (110, '王十二', 80, 70, 75, 80, 305)")
# 查询数学不及格的学生记录
c.execute("SELECT * FROM scores WHERE math < 60")
result = c.fetchall()
print("数学不及格的学生记录:")
for row in result:
print(row)
# 修改某一位学生的语文成绩
c.execute("UPDATE scores SET chinese = 95 WHERE student_id = 101")
# 删除数学不及格的学生记录
c.execute("DELETE FROM scores WHERE math < 60")
# 提交更改并关闭连接
conn.commit()
conn.close()
```