Cs50 Pset7房屋

时间:2020-08-13 22:19:05

标签: python sql cs50

我正在pset7的房屋上工作,但无法正常工作。基本上,我试图将数据从csv文件加载到SQL数据库中。每当我运行import.py时,我都会收到数据库错误。我不知道这是因为cs50提供的数据库,还是因为我的代码。

错误消息很长,但基本上它说数据库格式错误,无法加载数据

sqlite3.DatabaseError: malformed database schema

这是import.py的代码

import csv
import re
from sys import argv
from cs50 import SQL

#check for correct input
if not len(argv) == 2:
    print("usage: python import.py characters.csv")
    exit(1)

#make databaes
db = SQL("sqlite:///students.db")

#getting file
with open(argv[1], "r") as file:
    file = csv.DictReader(file, delimiter = ",")
    chars = list(file)
    #save the vars
    for row in chars:
        name = row["name"]
        house = row["house"]
        birth = row["birth"]
        full = name.split(" ")
    
    #if they dont have a midde name
    if len(full) == 2:
        first = full[0]
        last = full[1]
        db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?)",
        first, None, last, house, birth)
    
    #if they have a middle name
    elif len(full) == 3:
        first = full[0]
        middle = full[1]
        last = full[2]
        db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?)",
        first, middle, last, house, birth)  

0 个答案:

没有答案