0

I am new to flask and i am writing a basic program for login. Everytime I ammend i end with error mentioned above. below is my code for reference. Can someone please correct me.

@app.route('/')
def index():
    return render_template('form_ex.html')

@app.route('/',methods = ['POST'])
def Authenticate():

    login = request.form['u']
    password = request.form['p']
    cursor = mysql.get_db().cursor()
    cursor.execute("SELECT * FROM UserLogin WHERE login=%s and password=%s")
    data= cursor.fetchone()

    if data is None:

        return("Username or password incorrect")
    else:
        return("You are logged in")
Klaus D.
  • 13,874
  • 5
  • 41
  • 48
Divya Pai
  • 1
  • 3

1 Answers1

1

By the looks of the code you didn't initialise the MySQL DB, taken from this link the answer is below: Using MySQL in Flask

Firstly you need to install Flask-MySQL package. Using pip for example:

pip install flask-mysql

Next you need to add some configuration and initialize MySQL:

from flask import Flask
from flaskext.mysql import MySQL

app = Flask(__name__)
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'EmpData'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
Now you can get connection and cursor objects and execute raw queries:

conn = mysql.connect()
cursor =conn.cursor()

cursor.execute("SELECT * from User")
data = cursor.fetchone()
caterpree
  • 591
  • 2
  • 7
  • If you want to request additional data from the asking person please use the comments section instead of the answer section. – Klaus D. Dec 11 '18 at 10:39
  • @KlausD. oops sorry, new here so I didn't notice that, thanks for pointing it out! – caterpree Dec 11 '18 at 10:40
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/21656152) – AskNilesh Dec 11 '18 at 12:17
  • @NileshRathod Okay, so I should remove the answer then, according to Review and re-post with relevant info or...? – caterpree Dec 11 '18 at 12:56
  • 1
    Okay updated, thanks for pointers, as I highlighted, new here so not sure on the formalities yet :) – caterpree Dec 11 '18 at 13:01