How to set autocommit = 1 in sqlalchemy.engine.Connection

In sqlalchemy I am making a connection:

 conn = engine.connect()

      

I found that autocommit = 0 will be set in my mysqld log. Now I want to set autocommit = 1 because I do not want to request a transaction.

Is there a way to do this?

+4


source to share


4 answers


From SQLAlchemy Documentation: Understanding autocommit

conn = engine.connect()
conn.execute("INSERT INTO users VALUES (1, 'john')")  # autocommits

      

The autocommit function takes effect only when Transaction

not otherwise declared. This means that the function is usually not used with an ORM as the Session

default object always maintains the current one Transaction

.



Complete control over the behavior of "autocommit" available generator using the method Connection.execution_options()

provided in Connection

, Engine

, Executable

using "autocommit" flag that enables or disables the auto-update to the selected area. For example, a construct text()

representing a stored procedure that commits might use it so that a SELECT statement returns a COMMIT:

engine.execute(text("SELECT my_mutating_procedure()").execution_options(autocommit=True))

      

+4


source


You can use this:



from sqlalchemy.sql import text

engine = create_engine(host, user, password, dbname)
engine.execute(text(sql).execution_options(autocommit=True))

      

+4


source


What's your dialect for mysql connection?

You can install autocommit

to True

to solve the problem, for examplemysql+mysqldb://user:password@host:port/db?charset=foo&autocommit=true

+4


source


This can be done using an option autocommit

in the method execution_option()

:

engine.execute("UPDATE table SET field1 = 'test'").execution_options(autocommit=True))

      

This information is available in the documentation at Autocommit

0


source







All Articles