Skip to content

Instantly share code, notes, and snippets.

@Saarth-Jain
Created October 27, 2020 09:51
Show Gist options
  • Save Saarth-Jain/66705086d5b0b3d8c0aa932b605ed201 to your computer and use it in GitHub Desktop.
Save Saarth-Jain/66705086d5b0b3d8c0aa932b605ed201 to your computer and use it in GitHub Desktop.
CS50 Solution pset7 houses
# TODO START
from cs50 import SQL
from csv import reader
from sys import argv
if len(argv) != 2:
print(f"USAGE: {argv[0]} csv_file_name")
exit()
db = SQL("sqlite:///students.db")
with open(argv[1]) as csvfile:
characters_file = reader(csvfile)
for character in characters_file:
if character[0] == "name":
continue
name = character[0].split()
if len(name) == 2:
db.execute("INSERT INTO students(first,middle,last,house,birth)VALUES(?,?,?,?,?)",
name[0], None, name[1], character[1], character[2])
elif len(name) == 3:
db.execute("INSERT INTO students(first,middle,last,house,birth)VALUES(?,?,?,?,?)",
name[0], name[1], name[2], character[1], character[2])
# TODO END
# TODO START
from sys import argv
from cs50 import SQL
if len(argv) != 2:
print(f"USAGE: {argv[0]} HOUSE_NAME")
exit()
db = SQL("sqlite:///students.db")
students = db.execute("SELECT * FROM students WHERE house = (?) ORDER BY last", argv[1])
for student in students:
if student['middle'] != None:
print(f"{student['first']} {student['middle']} {student['last']}, born {student['birth']}")
else:
print(f"{student['first']} {student['last']}, born {student['birth']}")
# TODO END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment