Hi
My class which I've imported in the app.py is complaining that the con variable doesn`t exist.
Ok. I get that it thinks the variable doesn`t exist, but it does. before the route is called, it is set up.
When you import, does it evaluate the class at that time (if so then that is why.. the variable is only created after the import)
My class which I've imported in the app.py is complaining that the con variable doesn`t exist.
Ok. I get that it thinks the variable doesn`t exist, but it does. before the route is called, it is set up.
When you import, does it evaluate the class at that time (if so then that is why.. the variable is only created after the import)
from flask import Flask, render_template, flash, redirect, url_for, session, request from flask import send_from_directory, logging import os import pymysql import random from setup import setupADB app = Flask(__name__) app.debug = True app.config.from_object(__name__) app.config['SECRET_KEY'] = 'development key' con = pymysql.connect( host="localhost", user="root", password="thepwd", db="pyadventures", charset="utf8mb4", cursorclass=pymysql.cursors.DictCursor ) #the routes @app.route('/favicon.ico') def rFavicon(): return redirect(url_for('static', filename='favicon.ico'), code=302) @app.route('/') def rIndex(): return render_template("home.html") @app.route('/setup1time') def rSetupOneTime(): r = [] d = setupADB() d.createdb() r = d.dbtables() return 'l' #return render_template('setup.html',r=r) if __name__ == '__main__': app.run()and the class
class setupADB: # def __init__(self): #meh def createdb(self): ''' on the command line... mysql -u root -p grant all priveledges on *.* to 'thtename'@'localhost' identifiedby 'pwd'; \q mysql -u username -p create database nameofdatabase use nameofdatabase (or use the mysql manager, create a new schema (database) ) ''' def dbtables(self): r = [] sql = '' #complains on the following line - con should be global from the app.py??? with con.cursor() as cr: try: sql = """CREATE TABLE ... sql """ cr.execute(sql) except Exception as e: r.append("_su -> "+str(e)) try: sql = """CREATE TABLE ... sql """ cr.execute(sql) except Exception as e: r.append("stories -> "+str(e)) return r