movies_web_app.py - import os import time from flask import request from flask import Flask render_template import mysql.connector from mysql.connector

movies_web_app.py - import os import time from flask import...

This preview shows page 1 - 2 out of 4 pages.

import os import time from flask import request from flask import Flask, render_template import mysql.connector from mysql.connector import errorcode application = Flask(__name__) app = application def get_db_creds(): db = os.environ.get("DB", None) or os.environ.get("database", None) username = os.environ.get("USER", None) or os.environ.get("username", None) password = os.environ.get("PASSWORD", None) or os.environ.get("password", None) hostname = os.environ.get("HOST", None) or os.environ.get("dbhost", None) return db, username, password, hostname def create_table(): # Check if table exists or not. Create and populate it only if it does not exist. db, username, password, hostname = get_db_creds() table_ddl = ("CREATE TABLE `movies` (" " `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT," " `Year` INT UNSIGNED NOT NULL," " `Title` TEXT NOT NULL," " `Director` TEXT NOT NULL," " `Actor` TEXT NOT NULL," " `Release_Date` TEXT NOT NULL," " `Rating` FLOAT NOT NULL," " PRIMARY KEY (`ID`)" ")") cnx = '' try: cnx = mysql.connector.connect(user=username, password=password, host=hostname, database=db) except Exception as exp: print(exp) import MySQLdb #try: cnx = MySQLdb.connect(unix_socket=hostname, user=username, passwd=password, db=db) #except Exception as exp1: # print(exp1) cur = cnx.cursor() try: cur.execute(table_ddl) cnx.commit() except mysql.connector.Error as err: if err.errno == errorcode.ER_TABLE_EXISTS_ERROR: print("already exists.") else: print(err.msg) @app.route('/add_movie', methods=['POST']) def add_movie(): title = request.form['title']
Image of page 1
Image of page 2

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture