Search code examples
pythonmysqlflask-mysql

got response 200 but nothing happen (flask-mysql)


So I follow this tutorial, and after I reach step Connect Python to MySQL, I got 200 response code, but on postman I see this: { "error": "%d format: a number is required, not str" }

and then I check my table on MySQL, nothing happen, it still empty. please help me.

here is the code:

from flask import Flask
from flask_restful import Resource, Api, reqparse
from flaskext.mysql import MySQL

mysql = MySQL()
app = Flask(__name__)

app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
app.config['MYSQL_DATABASE_PORT'] = '5002'
app.config['MYSQL_DATABASE_DB'] = 'itemlistdb'

mysql.init_app(app)

api = Api(app)

class CreateUser(Resource):
    def post(self):
        try:
            parser = reqparse.RequestParser()
            parser.add_argument('email', type=str, help='Email address to create user')
            parser.add_argument('password', type=str, help='Password to create user')
            args = parser.parse_args()

            p_Username = args['email']
            p_Password = args['password']

            conn = mysql.connect()
            cursor = mysql.get_db().cursor()
            cursor.callproc('spCreateuser',(p_Username,p_Password))
            data = cursor.fetchall()

            if len(data) is 0:
                conn.commit()
                return {'statusCode':'200','Message': 'User creation success'}
            else:
                return {'StatusCode':'1000','Message': str(data[0])}

        except Exception as e:
            return {'error': str(e)}

class AuthenticateUser(Resource):
    def post(self):
        try:
            parser = reqparse.RequestParser()
            parser.add_argument('email', type=str, help='Email address for Authentication')
            parser.add_argument('password', type=str, help='Password for Authentication')
            args = parser.parse_args()

            p_Username = args['email']
            p_Password = args['password']

            conn = mysql.connect()
            cursor = conn.cursor()
            cursor.callproc('sp_AuthenticateUser',(p_Username,))
            data = cursor.fetchall()

            if(len(data)>0):
                if(str(data[0][2])==p_Password):
                    return {'status':200,'UserId':str(data[0][0])}
                else:
                    return {'status':100,'message':'Authentication failure'}

        except Exception as e:
            return {'error': str(e)}


api.add_resource(CreateUser, '/CreateUser')
api.add_resource(AuthenticateUser, '/AuthenticateUser')

if __name__ == '__main__':
    app.run(debug=True)

Screenshot with the error


Solution

  • I suspect that the issue isn't the call to create user, but the app connection itself.

    Specifically, I wonder about this:

    app.config['MYSQL_DATABASE_PORT'] = '5002'
    

    Server port numbers are normally integers (this isn't a Python thing, it's an architecture standard), so when MySQL tries to connect, it is probably doing something like:

    '<rest-of-the-connection:%d' % app.config['MYSQL_DATABASE_PORT']
    

    In that case, though, app.config['MYSQL_DATABASE_PORT'] is a str, not an int.

    Try

    app.config['MYSQL_DATABASE_PORT'] = 5002