import csv
import sys
import os
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy import create_engine
from flask import Flask, render_template, request, session
from flask_sqlalchemy import SQLAlchemy
from flask_session import Session

engine = create_engine(os.getenv("DATABASE_URL")) # database engine object from SQLAlchemy that manages connections to the database,# DATABASE_URL is an environment variable that indicates where the database lives

def main():
    f = open("movies.csv","r")
    reader = csv.reader(f)
    for row in f: # loop gives each column a name
        title,year,runtime,imdbID,imdbRating = row.split(';')
        engine.execute('INSERT INTO movies("title", "year", "runtime", "imdbID", "imdbRating") VALUES ((title), (year), (runtime), (imdbID), (imdbRating))',
                      {"title": title, "year": year, "runtime": runtime, "imdbID": imdbID, "imdbRating:": imdbRating }) # substitute values from CSV line into SQL command, as per this dict

    engine.commit() # transactions are assumed, so close the transaction finished
if __name__ == "__main__":
    main()
