如何解决Cs50 Pset7房屋
我正在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,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,birth)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。