how to set autocommit = 1 in a sqlalchemy.engine.Connection
What is your dialect for mysql connection?
You can set the autocommit
to True
to solve the problem, like this mysql+mysqldb://user:password@host:port/db?charset=foo&autocommit=true
From The SQLAlchemy documentation: Understanding autocommit
conn = engine.connect()
conn.execute("INSERT INTO users VALUES (1, 'john')") # autocommits
The âautocommitâ feature is only in effect when no Transaction
has otherwise been declared. This means the feature is not generally used with the ORM, as the Session
object by default always maintains an ongoing Transaction
.
Full control of the âautocommitâ behavior is available using the generative Connection.execution_options()
method provided on Connection
, Engine
, Executable
, using the âautocommitâ flag which will turn on or off the autocommit for the selected scope. For example, a text()
construct representing a stored procedure that commits might use it so that a SELECT statement will issue a COMMIT:
engine.execute(text("SELECT my_mutating_procedure()").execution_options(autocommit=True))
You can use this:
from sqlalchemy.sql import text
engine = create_engine(host, user, password, dbname)
engine.execute(text(sql).execution_options(autocommit=True))