The following is my code:
import csv
from sys import argv
import sqlite3
import os.path
def main():
#Check if correct number of arguments were provided by the user
if len(argv) != 2:
print("usage: python import.py characters.csv")
#opens up the students.db database file
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(BASE_DIR, "students.db")
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
with open(argv[1], 'r') as file:
character_file = csv.reader(file)
next(character_file)
for line in character_file:
#condition where all 3 names exist
if(line[0].count(" ") == 2):
name = line[0].split(" ")
first_name = name[0]
middle_name = name[1]
last_name = name[2]
student_house = line[1]
student_birth = int(line[2])
cursor.execute("INSERT INTO students (first, middle, last, house, birth) VALUES (?, ?, ?, ?, ?)", (first_name, middle_name, last_name, student_house, student_birth))
#condition where only the first and last name exist
else:
name = line[0].split(" ")
first_name = name[0]
middle_name = 'NULL'
last_name = name[1]
student_house = line[1]
student_birth = int(line[2])
cursor.execute("INSERT INTO students (first, middle, last, house, birth) VALUES (?, ?, ?, ?, ?)", (first_name, middle_name, last_name, student_house, student_birth))
main()
I am not sure if I understand why after executing this python program nothing is being inserted into my students.db database file. Can anyone please explain. When I check the .schema
I do see the structure of both of the tables that have been created by the staff, but inserting into it is not working.
I have looked at all of the various different questions posed on this pset, however, none of the solutions that have been proposed provide guidance on this specific matter.
Thank you.