0

The problem is this: When the command runs, it does not replace the data in the database.

I looked at countless pages but found no solution to the problem.

But it doesn't give an error.

Script:

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

    if 'loggedin' in session:
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM users WHERE id = %s', (session['id'],))
        account = cursor.fetchone()

        if request.method == 'POST':
            file = request.files['file']
            filename = secure_filename(file.filename)

            path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            file.save(path)

            cursor.execute('UPDATE users SET file_name = %s WHERE id = %s', (filename,account['id'],))

            return filename

        return render_template('profil.html', account=account)

    return redirect(url_for('login'))```
1
  • You need to call the connection's commit method to commit the change to the database. Commented Jun 13, 2021 at 10:37

1 Answer 1

1

When doing an UPDATE you need to commit explicitly with .commit:

cnx = mysql.connector.connect(...)
cursor = cnx.cursor()
cursor.execute('UPDATE .....')
cnx.commit()

https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-commit.html

You can autocommit with cnx.autocommit = True.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.