Skip to content

Instantly share code, notes, and snippets.

@cbscribe
Created January 20, 2021 18:06
Show Gist options
  • Save cbscribe/15d914e6f263d0b7c05a99b48ae4b5fe to your computer and use it in GitHub Desktop.
Save cbscribe/15d914e6f263d0b7c05a99b48ae4b5fe to your computer and use it in GitHub Desktop.
Python SQL example
from cs50 import SQL
db = SQL("sqlite:///students.db")
name = input("Name: ")
student_id = db.execute("INSERT INTO people (name) VALUES (?)", name)
course_id = int(input("Course id: "))
results = db.execute("SELECT id FROM courses WHERE id = ?", course_id)
if len(results) == 0:
print(f"No course number {course_id}")
else:
results = db.execute("INSERT INTO enrollment (course_id, person_id) VALUES (?, ?)", course_id, student_id)
print(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment