Search code examples
mysqljsonpython-3.xflaskflask-mysql

How i could Column names in response from cursor in Python Flask APP?


I´m developing a API on Python 3 using Flask and trying to insert data to Mysql, but on the response only comes the values and I can't show properly on the Json answer to app. Python Code

app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'didaxis'
mysql = MySQL(app)

@app.route('/insertAlumno', methods = ['POST'])
def insertAlumno():
    nombre = request.json['nombre']
    apellidoPaterno = request.json['apellidoPaterno']
    apellidoMaterno = request.json['apellidoMaterno']
    fechaNacimiento = request.json['fechaNacimiento']
    direccion = request.json['direccion']
    telefono = request.json['telefono']
    correo = request.json['correo']
    matricula = request.json['matricula']
    curso = request.json['curso']

    dataUser = (nombre, apellidoPaterno, apellidoMaterno, fechaNacimiento, direccion, telefono, correo, matricula, curso)

    conect = mysql.connection.cursor()
    conect.callproc('insertarAlumno', dataUser)
    data = conect.fetchall()
    data=jsonify(data)

    return data

Stored Procedure

DELIMITER //
CREATE PROCEDURE insertarAlumno(_nombre VARCHAR(50),
                                _apellidoPaterno VARCHAR(30),
                                _apellidoMaterno VARCHAR(30),
                                _fechaNacimiento DATE,
                                _direccion VARCHAR(100),
                                _telefono VARCHAR(20),
                                _correo VARCHAR(50),
                                _matricula VARCHAR(50),
                                _curso VARCHAR(10))
BEGIN
    DECLARE idPersonaTemp,idUserTemp INT DEFAULT 0;
    
    INSERT INTO persona (nombre,apellidoPaterno,apellidoMaterno,fechaNacimiento,direccion,telefono,correo) 
    VALUES (_nombre,_apellidoPaterno,_apellidoMaterno,_fechaNacimiento,_direccion,_telefono,_correo);
    
    SET idPersonaTemp = last_insert_id();
    
    INSERT INTO usuario (usuario,pass,estatus) 
    VALUES (_matricula,_matricula,1);
    
    SET idUserTemp = last_insert_id();
    
    INSERT INTO alumno VALUES (_matricula,idPersonaTemp,idUserTemp,_curso,1);
    
    SELECT * FROM vista_alumnos WHERE id = _matricula;

END //

Response that I get from Postman

[
    [
        "Simon",
        "Lopez",
        "Lopez",
        "Fri, 23 Oct 1998 00:00:00 GMT",
        "Miguel Hidalgo 515",
        "4761138167",
        "[email protected]",
        "178724",
        "178724",
        "178724",
        9,
        9,
        "1APre",
        1
    ]
]

I hope that some one can explain me about this issue and how i could get one response json with column name to identify values.


Solution

  • I got the answer add a few lines on the Python Code

        conect = mysql.connection.cursor()
        conect.callproc('insertarAlumno', dataUser)
        *columns=[x[0] for x in conect.description]
        data = conect.fetchall()
        *json_data=[]
        *for result in data:
            *json_data.append(dict(zip(columns,result)))
       
        *return jsonify(json_data)
    

    The lines that starts with a * was modified vs the post or just was added.