Search code examples
pythonmysqlflasktypeerrormysql-python

Flask: TypeError: 'str' object cannot be interpreted as an integer


So, I've been trying to add new records in a table of a database from XAMPP using Flask but I can't make this code work. Here's my code:

from flask import Flask, render_template, request
from flask_mysqldb import MySQL

app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_PORT'] = '3307'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'password'
app.config['MYSQL_DB'] = 'pruebas'
mysql = MySQL(app)

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

@app.route('/add contact', methods=['POST'])
def add_contact():
    if request.method == 'POST':
        Nombre = request.form['Nombre']
        Estado = request.form['Estado']
        Situacion = request.form['Situacion']
        cur = mysql.connection.cursor()
        cur.execute('INSERT INTO prueba1 (Nombre, Estado, Situacion) VALUES (%s, %s, %s)', (Nombre, Estado, Situacion))
        mysql.connection.commit()
        return 'Recibido'
    
if __name__ == '__main__':
    app.run(port = 3000, debug = True)

I've already try putting a the name of the columns before the %s but it doesn't work either.

cur.execute('INSERT INTO prueba1 (Nombre, Estado, Situacion) VALUES (%(Nombre)s, %(Estado)s, %(Situacion)s)', (Nombre, Estado, Situacion))
mysql.connection.commit()

I link three imgs, the first one is the columns MySQL configuration, the second one is an example of how the data looks like in the table and the final one is the traceback of the error. Thanks for the help, I'm a beginner.

https://i.sstatic.net/LCLrz.png https://i.sstatic.net/ZUQXP.png https://i.sstatic.net/rjWgg.png


Solution

  • The port number must be an integer, not a string.

    app.config['MYSQL_PORT'] = 3307