问题描述
我正在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 (将#修改为@)