2

I'm using flask to build a simple web app but for whatever reason the conn.commit() is not committing the data into the database. I know this because when I manually add something to the database the data doesn't change but the ID section increases each time I test it (because its using auto increment). So basically my current table has ID 1, Username test, Password test and the next entry that I inserted manually (after trying to use my application) was ID 5, Username blah, Password blah. Is there any specific reason that the commit isn't working?

EDIT: I had to change cursor = mysql.connect().cursor() to conn.cursor()

@app.route('/add_data/')
def add_tv_to_database():
    conn = mysql.connect()
    cursor = mysql.connect().cursor()
    cursor.execute("INSERT INTO _accounts VALUES (null, 'test','test')")
    conn.commit()
    return render_template('index.html')
4
  • Does the commit cause an exception? Commented Nov 7, 2014 at 20:31
  • no its running fine and has no exceptions it just for whatever reason doesn't want to post the data into the table Commented Nov 7, 2014 at 20:34
  • 1
    Ok, I just got it working and updated the post, all I had to do was change cursor = mysql.connect().cursor() to cursor = conn.connect() what was happening was I was establishing two different connections to the database so I was unable to update! Commented Nov 7, 2014 at 21:10
  • Hey @josh. I'm getting the AttributeError 'Connection' object has no attribute 'connect'. Did you get this too? I tried your changes. You know what's up? Commented Nov 23, 2014 at 19:13

1 Answer 1

2

In the fourth 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.

Comments

Your Answer

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