-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.py
27 lines (23 loc) · 856 Bytes
/
import.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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()