尝试将 CSV 文件导入在线 psql 数据库时出错
Error when trying importing CSV file to online psql database
错误:sqlalchemy.exc.ProgrammingError:(psycopg2.errors.UndefinedColumn)列“isbn
关系 "books" 不存在
LINE 1: INSERT INTO books (isbn, title, author, publicationyear) VAL...
PYTHON CODE:
import csv
import os
from flask import Flask, render_template, request
from models import *
from application import DATABASE_URL
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URL
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
def main():
f = open("books.csv")
reader = csv.reader(f, delimiter=',')
next(reader) # skips the top line
for isbn, title, author, date in reader:
book = Book(isbn = isbn, title = title, author = author, publicationyear = int(date))
db.session.add(book)
print(f"Added book with {isbn}, title : {title}, author: {author}, publication year: {date}.")
print("commiting")
db.session.commit()
print("committed all data")
if __name__ == "__main__":
with app.app_context():
main()
看来您的模型文件可能有问题。错误是说 books
不存在,你确定你已经创建了你的书 table 吗? db.create_all()
您的模型应该位于 table 定义所在的位置。我建议您在开始使用 Python-Flask-SQLAlchemy 之前使用 SQL 命令成为 comfortable。希望这有帮助。
错误:sqlalchemy.exc.ProgrammingError:(psycopg2.errors.UndefinedColumn)列“isbn 关系 "books" 不存在 LINE 1: INSERT INTO books (isbn, title, author, publicationyear) VAL...
PYTHON CODE:
import csv
import os
from flask import Flask, render_template, request
from models import *
from application import DATABASE_URL
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URL
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
def main():
f = open("books.csv")
reader = csv.reader(f, delimiter=',')
next(reader) # skips the top line
for isbn, title, author, date in reader:
book = Book(isbn = isbn, title = title, author = author, publicationyear = int(date))
db.session.add(book)
print(f"Added book with {isbn}, title : {title}, author: {author}, publication year: {date}.")
print("commiting")
db.session.commit()
print("committed all data")
if __name__ == "__main__":
with app.app_context():
main()
看来您的模型文件可能有问题。错误是说 books
不存在,你确定你已经创建了你的书 table 吗? db.create_all()
您的模型应该位于 table 定义所在的位置。我建议您在开始使用 Python-Flask-SQLAlchemy 之前使用 SQL 命令成为 comfortable。希望这有帮助。