第993741题 程序题
学生数据库

设计一个SQLite数据库应用,实现以下功能:

创建一个名为students的表,该表具有id(主键,自增)、name、age和grade四个字段。

①插入5条学生记录。

②修改一个学生的年龄。

③删除一个学生记录。

④查询所有学生的信息。

请根据要求,补全代码。(本题无需运行通过,写入代码即可)

import sqlite3

连接到SQLite数据库(如果数据库不存在,则会创建一个新的数据库)

conn = sqlite3.connect('students.db')

cursor = ①

创建students表

cursor.execute('''CREATE TABLE IF NOT EXISTS students (

id INTEGER PRIMARY KEY AUTOINCREMENT,

name TEXT NOT NULL,

age INTEGER,

grade TEXT)

''')

插入5条学生记录

students = [

('Alice', 20, 'A'),

('Bob', 2, 'B'),

('Charlie', 19, 'C'),

('David', 21, 'A'),

('Eva', 23, 'B')

]

cursor.executemany('''INSERT INTO students (name, age, grade) VALUES (?, ?, ?)

''', ② )

修改一个学生的年龄(例如修改id为2的学生的年龄为24)

cursor.execute("UPDATE students SET age = 24 WHERE id = 2")

删除一个学生记录(例如删除id为3的学生)

cursor.execute("DELETE FROM students WHERE id = 3")

③

查询所有学生信息

cursor.execute('SELECT * FROM students')

print("所有学生信息:")

for row in ④ :

print(row)

关闭数据库连接

conn.close()

{{ option.label }}
子题{{ index + 1 }} {{ child.type_label }}
{{ option.label }}
✓ 正确 ◐ 部分正确 ✗ 错误
编辑模式
程序运行统计
暂无判题统计
知识点:
提交{{ questionInfo.stats ? questionInfo.stats.submit_count : 0 }}次 正确率{{ statsAccuracy }}%
答案解析