使用Python sqlite3库完成学生数据库操作并补全代码
类型:程序题

使用Python的sqlite3库完成以下需求并补全代码:

  1. 创建一个名为students的数据库;
  2. 在该数据库中创建名为students_table的表,包含字段:id(主键)、name(学生姓名)、age(学生年龄)、grade(学生年级);
  3. students_table中插入至少5个学生的数据;
  4. 查询年龄大于18岁的所有学生并打印结果;
  5. 将名字为Alice的学生的年龄增加1岁;
  6. 删除名字为Bob的学生。

(本题无需运行通过,写入代码即可)

import sqlite3
conn = sqlite3.connect('①')
cursor = conn.cursor()
cursor.execute('''
② students_table(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,age INTEGER,grade TEXT)''')
students = [('Alice', 17, '10th'), ('Bob', 18, '11th'), ('Charlie', 16, '10th'), ('David', 19, '12th'), ('Eve', 17, '11th')]
cursor.executemany('''INSERT INTO students_table (name, age, grade) VALUES (?, ?, ?)''', students)
conn.commit()
cursor.execute('SELECT * FROM students_table ③')
print("年龄大于18岁的学生:")
print(cursor.④)
cursor.execute('UPDATE students_table SET age = age + 1 WHERE name = "Alice"')
cursor.execute('DELETE FROM students_table WHERE name = "Bob"')
conn.commit()
conn.close()
代码编辑器 加载中...
测试用例(F10) 运行测试(F11) 提交答案(F12)
测试用例输入
{{resultStatus.text}}