0

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.

1
  • I have even tried using the method provided by in the course by using the following: open(f"students1.db", "w") db = cs50.SQL("sqlite:///students1.db") db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES (?, ?, ?, ?, ?)", (first_name, middle_name, last_name, student_house, student_birth)). Over here the error I get is RuntimeError: no such table: students which is weird since .schema shows that there is a table called students. Can someone please provide some guidance on this. Thanks May 23, 2020 at 16:53

1 Answer 1

1

Since program uses the sqlite3 python interface (not the CS50 SQL interface), any changes to the database need to be committed in order to persist. From the python sqlite interface doc:

commit()

This method commits the current transaction. If you don’t call this method, anything you did since the last call to commit() is not visible from other database connections. If you wonder why you don’t see the data you’ve written to the database, please check you didn’t forget to call this method

.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .