导入csv并上传到数据库错误(flask-sqlalchemy)

时间:2020-06-25 14:27:55

标签: python sql database flask-sqlalchemy cs50

我正在研究CS50课程的project1,在尝试将csv文件中的数据写入heroku服务器中的数据库时被卡住了。我在flask应用程序的主文件中使用了相同的数据库,并且可以正常工作。

import csv
from models import *
from flask import Flask

app = Flask(__name__)
app.secret_key = "Needs_to_change"

app.config["SQLALCHEMY_DATABASE_URI"] = 'postgres://jcvwmqhjhvpyvf:6341f6696618f90b07a0f08cd3d73738b6ce4a4f837bd89a9e6bb6532ad19ad7@ec2-52-72-221-20.compute-1.amazonaws.com:5432/dd95sk0modea7s'
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

db = SQLAlchemy(app)

with open('books.csv', 'r') as books:
    csv_reader = csv.reader(books)
    for count, row in enumerate(csv_reader):
        if count > 0:
            data = Books(isbn=row[0], title=row[1], author=row[2], year=row[3])
            db.session.add(data)
            db.session.commit()

我得到的错误是:

[SQL: INSERT INTO books (isbn, title, author, year) VALUES (%(isbn)s, %(title)s, %(author)s, %(year)s)]
[parameters: {'isbn': '0380795272', 'title': 'Krondor: The Betrayal', 'author': 'Raymond E. Feist', 'year': '1998'}]
(Background on this error at: http://sqlalche.me/e/f405)

0 个答案:

没有答案
相关问题