Final challenge: the marks book
After this: Predict and explain what a marks-book program prints when it reports on courses and changes marks.
The marks book is a program on the school database from the earlier tutorials: teachers, students, courses, and enrolments with a mark for each student in each course. It loads the database into memory from the file school.sql, then uses two functions. course_report(title)courseReport(title) prints how many marks a course has and their average, with a LEFT JOIN from courses to enrolments and the title in a placeholder. set_mark(student_id, course_id, mark)setMark(studentId, courseId, mark) changes one mark inside a transaction and returns how many rows it changed, or 0 when the database refuses.
import sqlite3
conn = sqlite3.connect(":memory:")with open("school.sql") as f: conn.executescript(f.read())
REPORT = """SELECT COUNT(enrolments.mark), AVG(enrolments.mark)FROM coursesLEFT JOIN enrolments ON enrolments.course_id = courses.idWHERE courses.title = ?"""
def course_report(title): count, average = conn.execute(REPORT, (title,)).fetchone() print(count, average)
def set_mark(student_id, course_id, mark): sql = "UPDATE enrolments SET mark = ? WHERE student_id = ? AND course_id = ?" try: with conn: cur = conn.execute(sql, (mark, student_id, course_id)) return cur.rowcount except sqlite3.IntegrityError as e: print("refused:", e) return 0
course_report("History")print(set_mark(3, 3, 74))course_report("History")print(set_mark(3, 3, 140))print(set_mark(5, 3, 60))course_report("Music")course_report("Drama")import java.nio.file.Files;import java.nio.file.Path;import java.sql.*;
public class Main { static Connection conn;
static final String REPORT = "SELECT COUNT(enrolments.mark), AVG(enrolments.mark) " + "FROM courses " + "LEFT JOIN enrolments ON enrolments.course_id = courses.id " + "WHERE courses.title = ?";
static void courseReport(String title) throws SQLException { PreparedStatement ps = conn.prepareStatement(REPORT); ps.setString(1, title); ResultSet rs = ps.executeQuery(); rs.next(); System.out.println(rs.getInt(1) + " " + rs.getObject(2)); }
static int setMark(int studentId, int courseId, int mark) throws SQLException { String sql = "UPDATE enrolments SET mark = ? WHERE student_id = ? AND course_id = ?"; conn.setAutoCommit(false); try { PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, mark); ps.setInt(2, studentId); ps.setInt(3, courseId); int changed = ps.executeUpdate(); conn.commit(); return changed; } catch (SQLException e) { System.out.println("refused: " + e.getMessage()); conn.rollback(); return 0; } }
public static void main(String[] args) throws Exception { conn = DriverManager.getConnection("jdbc:sqlite::memory:"); conn.createStatement().executeUpdate(Files.readString(Path.of("school.sql"))); courseReport("History"); System.out.println(setMark(3, 3, 74)); courseReport("History"); System.out.println(setMark(3, 3, 140)); System.out.println(setMark(5, 3, 60)); courseReport("Music"); courseReport("Drama"); }}This challenge has five steps, and each step is scored on its own. They follow the calls at the bottom of the program in order, so each step starts from where the one before left the data. Every step reads the same in Python and in Java. You can try as many times as you like, and hints work as they do in the lessons.
History has two students: Ana with 80, and Chloe, whose mark is NULL because it is not set yet. What does the report print?
course_report("History")courseReport("History");Chloe is student 3 and History is course 3. The program sets her mark to 74, then reports on History again. What do the two lines print?
print(set_mark(3, 3, 74))course_report("History")System.out.println(setMark(3, 3, 74));courseReport("History");The enrolments table has the rule CHECK (mark BETWEEN 0 AND 100). Chloe's History mark is 74. What happens with this call?
print(set_mark(3, 3, 140))System.out.println(setMark(3, 3, 140));Eve, student 5, takes no courses. What does this call print?
print(set_mark(5, 3, 60))System.out.println(setMark(5, 3, 60));Music is a course with no students. There is no course called Drama. Both reports print 0 Nonenull. How could the program tell a course with no students from a course that does not exist?
course_report("Music")course_report("Drama")courseReport("Music");courseReport("Drama");- Predict a report built with a LEFT JOIN, COUNT and AVG, with NULL marks skipped.
- Say what an UPDATE inside a transaction returns: the number of rows it changed.
- Say what happens when a change breaks a CHECK rule: refused, rolled back, and 0 rows changed.
- Tell a course with no students from a course that does not exist by asking the courses table first.