TRUNCATE sql statement

3,541 views
Skip to first unread message

RVince

unread,
Jun 2, 2011, 2:45:02 PM6/2/11
to sqlalchemy
I am trying to issue a straight SQL TRUNCATE table command:

engine = engine_from_config(config, 'sqlalchemy.')
connection = engine.connect()
connection.execute('truncate table cms_history;')

Everything runs fine without error -- but the table does not truncate
-- nothing is affected. Am I missing something obviouis to others in
trying to do this? Thanks, RVince

RVince

unread,
Jun 2, 2011, 3:16:08 PM6/2/11
to sqlalchemy
I found the answer. I needs to be wrapped in a transaction, like this:

engine = engine_from_config(config, 'sqlalchemy.')
connection = engine.connect()
trans = connection.begin()
try:
connection.execute('truncate table cms_history;')
trans.commit()
except :
trans.rollback()

Michael Bayer

unread,
Jun 2, 2011, 3:24:09 PM6/2/11
to sqlal...@googlegroups.com
not to answer every question today, but we also added an "autocommit" flag for this kind of thing:

connection.execution_options(autocommit=True).execute("statement that normally isn't autocommit")

some background:

http://www.sqlalchemy.org/docs/core/connections.html#understanding-autocommit

> --
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
>

Reply all
Reply to author
Forward
0 new messages