6
engine = create_engine('postgresql://username:password@host:5432/database')
transactions.to_sql('transactions', engine,if_exists='append',index=False,method='multi')

How do I rollback this?

1 Answer 1

12

You can begin a transaction before calling to_sql and then do a rollback afterwards:

import pandas as pd
import sqlalchemy as sa

engine = sa.create_engine("mssql+pyodbc://@mssqlLocal64")

def dump_tran_test_table(conn):
    print(conn.execute(sa.text("SELECT * FROM tran_test")).fetchall())

# set up test environment
with engine.begin() as conn:
    conn.exec_driver_sql("DROP TABLE IF EXISTS tran_test")
    conn.exec_driver_sql(
        "CREATE TABLE tran_test "
        "(txt varchar(10), id int primary key)"
    )
    conn.exec_driver_sql(
        "INSERT INTO tran_test (txt, id) VALUES "
        "('old_foo', 1), ('old_bar', 2)"
    )

# test
with engine.connect() as conn:
    tran = conn.begin()
    df = pd.DataFrame([("new_baz", 3)], columns=["txt", "id"])
    df.to_sql("tran_test", conn, index=False, if_exists="append")
    dump_tran_test_table(conn)
    """console output:
    [('old_foo', 1), ('old_bar', 2), ('new_baz', 3)]
    """
    tran.rollback()
    dump_tran_test_table(conn)
    """console output:
    [('old_foo', 1), ('old_bar', 2)]
    """
Sign up to request clarification or add additional context in comments.

6 Comments

Hey gord, this looks good will try this. One doubt I had was that after df.to_sql here, do I need to commit this or it gets automatically committed?
If you explicitly .begin a transaction then yes, you need to explicitly .commit it.
Hello Gord, could you show where in this code we need to commit it?
Doesn't work anymore "AttributeError: 'Connection' object has no attribute 'connect'" when you pass in conn to .to_sql
Hi @TomMedhurst - I just tested this example with SQLAlchemy 2.0.19 and pandas 2.0.3 and it worked fine. What versions are you running?
|

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.