i'm using flask and flask-mysqldb each time i want to create or execute a sql query the cursor should be inside the flask route i want to use the cursor in another function not in a route also i want to define a single cursor so i don't have to type the same code in each roue here is my code :

from flask import Flask, render_template, request
from flask_mysqldb import MySQL
import yaml
import json

app = Flask(__name__)

# config the database connection
db = yaml.safe_load(open('db.yaml'))
app.config['MYSQL_HOST'] = db['mysql_host']
app.config['MYSQL_USER'] = db['mysql_user']
app.config['MYSQL_PASSWORD'] = db['mysql_password']
app.config['MYSQL_DB'] = db['mysql_db']

mysql = MySQL(app)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # get the inputs from the form
        user_details = request.form
        # the key value is from the html name attriput
        name = user_details['name']
        email = user_details['email']

        cur = mysql.connection.cursor()
            " INSERT INTO users(name,email ) VALUES(%s, %s)", (name, email))
        return 'success'
    return render_template('index.html')

def users():

    cur = mysql.connection.cursor()
    result_value = cur.execute(" SELECT * FROM users ")
    if result_value > 0:
        user_details = cur.fetchall()
        return render_template('users.html', user_details=user_details)

#convert users data to json file
def json_conv():
    cur = mysql.connection.cursor()
    cur.execute("SHOW GLOBAL STATUS")
    rs = cur.fetchall()

    result = dict(rs)

    with open('users.json', 'w') as f:
        json.dump(result, f)


if __name__ == '__main__':

NOTE : if i run the function this error pop up :

Traceback (most recent call last):
  File "app.py", line 74, in <module>
  File "app.py", line 64, in json_conv
    cur = mysql.connection.cursor()
AttributeError: 'NoneType' object has no attribute 'cursor'

0 Answers