Created
January 20, 2021 18:06
-
-
Save cbscribe/15d914e6f263d0b7c05a99b48ae4b5fe to your computer and use it in GitHub Desktop.
Python SQL example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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