Skip to content

Instantly share code, notes, and snippets.

@simonrw
Created September 18, 2015 19:01
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save simonrw/664924f7a0e4e961aea9 to your computer and use it in GitHub Desktop.
Save simonrw/664924f7a0e4e961aea9 to your computer and use it in GitHub Desktop.
Notes about pymysql connections

Database transactions

pymysql

  • Defaults to autocommit=False
connection = pymysql.connect(user='user', db='test')
cursor = connection.cursor()
cursor.execute('insert into test (value) values (10)')
connection.close()

connection = pymysql.connect(user='user', db='test')
cursor = connection.cursor()
cursor.execute('select value from test')
# => []

To commit changes to the database, #commit() must be called:

connection = pymysql.connect(user='user', db='test')
cursor = connection.cursor()
cursor.execute('insert into test (value) values (10)')
# Call the commit line
connection.commit()
connection.close()

connection = pymysql.connect(user='user', db='test')
cursor = connection.cursor()
cursor.execute('select value from test')
# => [(10, )]

With a context manager on a connection, an implicit transaction is opened:

with pymysql.connect(user='user', db='test') as cursor:
    cursor.execute('insert into test (value) values (10)')

with pymysql.connect(user='user', db='test') as cursor:
    cursor.execute('select value from test')
    # => [(10, )]

The connect function includes an autocommit parameter:

connection = pymysql.connect(user='user', db='test', autocommit=True)
cursor = connection.cursor()
cursor.execute('insert into test (value) values (10)')
connection.close()

connection = pymysql.connect(user='user', db='test')
cursor = connection.cursor()
cursor.execute('select value from test')
# => [(10, )]

Multiple cursors can see any changes made within a transaction

connection = pymysql.connect(user='user', db='test')
cursor = connection.cursor()
cursor2 = connection.cursor()
cursor.execute('insert into test (value) values (10)')
cursor2.execute('select value from test')
# => [(10, )]

Multiple connections are isolated from each other

connection = pymysql.connect(user='user', db='test')
connection2 = pymysql.connect(user='user', db='test')
cursor = connection.cursor()
cursor2 = connection2.cursor()
cursor.execute('insert into test (value) values (10)')
cursor2.execute('select value from test')
# => []
@MrFrankLiao
Copy link

thanks for your sharing!

Copy link

ghost commented Nov 15, 2017

Can I find the connection as an attribute of a cursor?

Thanks

@RaphaelRevivor
Copy link

RaphaelRevivor commented Aug 27, 2018

Thanks for sharing! A question: since multi-connections are isolated from each other, what should I do to let the 2nd connection get the changes done by the 1st connection? ( In my case, do a commit in conn 1 after insert, then select in conn 2 doesn't work; but do a commit in conn 2 before select works. It doesn't look like a good solution tho...) Many thanks!

@huanganqing
Copy link

Just wondering if any way to achieve

START TRANSACTION;
SELECT quantity FROM product WHERE product_id = 5 FOR UPDATE;
UPDATE product SET quantity = quantity - 1 WHERE product_id = 5;
COMMIT;

@FreeFly19
Copy link

FreeFly19 commented Sep 16, 2019

Just wondering if any way to achieve

START TRANSACTION;
SELECT quantity FROM product WHERE product_id = 5 FOR UPDATE;
UPDATE product SET quantity = quantity - 1 WHERE product_id = 5;
COMMIT;

Hi, you can connection.begit for this:

connection = pymysql.connect(user='user', db='test')
connection.begin()
connection.cursor().execute('SELECT quantity FROM product WHERE product_id = 5 FOR UPDATE;')
connection.cursor().execute('UPDATE product SET quantity = quantity - 1 WHERE product_id = 5;')
connection.commit()
connection.close()

@BradAxl
Copy link

BradAxl commented Oct 6, 2021

Thanks for sharing! A question: since multi-connections are isolated from each other, what should I do to let the 2nd connection get the changes done by the 1st connection? ( In my case, do a commit in conn 1 after insert, then select in conn 2 doesn't work; but do a commit in conn 2 before select works. It doesn't look like a good solution tho...) Many thanks!

You should commit() the insertion with first connection and you can access it with second connection without any problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment