flask + mysql beginner issue
<p>This is my first ever SO post, so goes without saying I am only just starting with web programming. Did a lot of Pascal programming in high school, but that was long ago. Please bear with me.</p>

<p>I am trying to create a simple web app and the first thing I am building is the register feature. Using flask and mysql as my database.</p>

<p>After googling I couldn't find a solution to my problem, so had to ask here, apologies if it was asked before.</p>

<h2>Here is my code:</h2>

<pre><code>from flask import Flask, request
from flaskext.mysql import MySQL

app = Flask(__name__)

mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'mmoosakam93#'
app.config['MYSQL_DATABASE_DB'] = 'profApp'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)

@app.route('/')
def hello():
    return "Welcome to my world!"

@app.route('/Authenticate')
def authenticate():
    username = request.args.get('userName')
    password = request.args.get('password')
    email = request.args.get('email')
    cursor.execute("Select * from user where userName='" + "'userName + and Password='" + password + "'")
    data = cursor.fetchone()
    if data is None:
        return "Usernamee or Password is wrong"
    else:
        return "Logged in successfully"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)
</code></pre>

<hr>

<p>When i run it from my terminal, after manually inputting parameters into my browser (<a href="http://192.168.33.33:5000/Authenticate?userName=jay&amp;password=jay&amp;email=aoisf" rel="nofollow">http://192.168.33.33:5000/Authenticate?userName=jay&amp;password=jay&amp;email=aoisf</a>) I get the following error:</p>

<p>"  File "app.py", line 25, in authenticate
    cursor.execute("Select * from user where userName='" + "'userName + and Password='" + password + "'")
NameError: global name 'cursor' is not defined  "</p>

<p>I understand that 'cursor' is a variable which I haven't defined properly, but I don't know how to do this.</p>

<p>Thank you in advance!</p>

<p>You need to add define <code>cursor</code> variable in <code>authenticate()</code> controller like this</p>

<pre><code>cursor = mysql.connection.cursor()  # for Flask-MySQLdb package (not relevant here)
</code></pre>

<p>Also you need to define it before it's first use.</p>

<p><strong>UPDATE</strong> since you are  using <a href="http://flask-mysql.readthedocs.io/" rel="nofollow">Flask-MySQL</a> to fetch cursor you need to run </p>

<pre><code>cursor = mysql.get_db().cursor()
</code></pre>

<p>By defining <code>cursor</code> before it first use i meant that you need to place definition line before that line</p>

<pre><code>cursor.execute("Select * from user where userName='" + "'userName + and Password='" + password + "'")
</code></pre>

python mysql flask