简体   繁体   中英

How to rollback dataframe.to_sql in python in SQLALCHEMY?

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?

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())

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

    # test
    df = pd.DataFrame([("new_baz", 3)], columns=["txt", "id"])
    tran = conn.begin()
    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)]
    """

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM