""" registrar.py A short example of calling sqlite from python. See docs.python.org/3/library/sqlite3.html and cs.bennington.college/courses/spring2021/algorithms/code/sql/registrar.sql Jim Mahoney | cs.bennington.college | April 2022 | MIT License """ import sqlite3 student_name = "River Song" db_file = "./registrar.db" db = sqlite3.connect(db_file) # open the database file cursor = db.cursor() # a (stateful) object that cursor.execute("SELECT id FROM Student WHERE name=?", (student_name,)) # Note that you should NOT just insert student_name into the sql string. # For example, this next statement would be a SECURITY ERROR that allows # SQL INJECTION ATTACKS ... so DON'T DO THIS : # cursor.execute(f'SELECT id FROM Student WHERE name="{student_name}";') # That syntax will work ... but might expand into BAD STUFF. student_row = cursor.fetchone() # fetch one row tuple (a,b,c) result student_id = student_row[0] # id is 0'th (only) data in this row print(f"Student '{student_name}' has id={student_id}.")