Flask mySQLDB execute success or failure Flask mySQLDB execute success or failure flask flask

Flask mySQLDB execute success or failure


c.execute(qry) in your case won't return anything. So, You can't use it for if statement.

However, You could use fetchall() or fetchone() to check if there are some results for your query. It would look something like this:

qry = "SELECT count(*) FROM users where username = (%s)" % (username)try:    c.execute(qry) #Is this correct? Doe execute command return a value?    user = c.fetchone()    if user:        return 'Success'    else:        return 'Failure'except Exception as e:    return 'Failure'


You already have the means for detecting the failure in the code you have posted. If something goes wrong with the query, Python will raise an exception, which you can catch. Although it's worth noting that you should never catch a bare Exception, and you should certainly never hide what that exception is; catch the relevant MySQLdb exceptions, eg MySQLdb.Error.