70

I'm running this from PyDev in Eclipse...

import pymysql
conn = pymysql.connect(host='localhost', port=3306, user='userid', passwd='password', db='fan')
cur = conn.cursor()
print "writing to db"
cur.execute("INSERT INTO cbs_transactions(leagueID) VALUES ('test val')")
print "wrote to db"

The result is, at the top of the Console it says C:...test.py, and in the Console:

writing to db wrote to db

So it's not terminating until after the execute command. But when I look in the table in MySQL it's empty. A record did not get inserted.

First off, why isn't it writing the record. Second, how can I see a log or error to see what happened. Usually there should be some kind of error in red if the code fails.

1

3 Answers 3

140

Did you commit it? conn.commit()

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

1 Comment

you're my hero!
126

PyMySQL disable autocommit by default, you can add autocommit=True to connect():

conn = pymysql.connect(
    host='localhost',
    user='user',
    passwd='passwd',
    db='db',
    autocommit=True
)

or call conn.commit() after insert

5 Comments

Thanks. I was looking for autocommit example. Excellent!
guys in 2019 this is not working when using pymysql in python3.x where's this project so that we can create an issue?
Something I could not find easily was that you can change the autocommit property later with the method connection.autocommit(True)
This is really helpful, but I cannot believe that they would not do this by default!
in the documentation there is no 'autocommit' parameter when calling the 'pymysql.connect' function pymssql.org/ref/pymssql.html#pymssql.connect
4

You can either do

  • conn.commit() before calling close

or

  • enable autocommit via conn.autocommit(True) right after creating the connection object.

Both ways have been suggested from various people at a duplication of the question that can be found here: Database does not update automatically with MySQL and Python

Comments

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.