0

I'm trying to get a small Flask app working that writes to a MySQL database, but it seems like the database commit isn't working and I have no idea why.

from flaskext.mysql import MySQL

mysql = MySQL()
mysql.init_app(app)

app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'eggsited'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'

conn = mysql.connect()
cursor = mysql.connect().cursor()

cursor.execute('insert into user (id, first_name, last_name) values (%s, %s, %s)', ['324325', 'Kristoffer', 'Nielsen'])
conn.commit()

That's the part that deals with inserting values into my table. I know that my connection is working since I can do select statements and list the results from those and the execute statement is working too, because when I insert a new row directly in mysql the auto incremented id field has been incremented. So it must be the commit command that isn't actually committing the execute statement, but I have no idea why.

Is there something obvious that I have missed?

2
  • 3
    "isn't working" as in you're getting an error message or "isn't working" as in my data isn't in the DB? Commented Apr 30, 2015 at 20:08
  • Oh, sorry. Should have explained that better. It's not working as in my data isn't in the DB. I'm not getting any error messages. Commented Apr 30, 2015 at 20:15

1 Answer 1

5

From this question: MySQL not updating after calling connection.commit() with Flask (WORKING)

In the third last line of your code, change it from cursor = mysql.connect().cursor() to cursor = conn.cursor(). This will ensure that the cursor uses the existing connection to database (from the previous line of code), instead of creating a new MySQL connection.

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

1 Comment

This is exactly what's wrong! Thank you so much :-) It makes a lot of sense the way you explained it too!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.