Home > Net >  Python pandas how many rows insert into DB
Python pandas how many rows insert into DB

Time:03-02

I use the TO_SQL function from pandas to insert data from DF into a table in Oracle. I would like to check after completing the action the amount of records append to the table. can it be done and how? Thanks

CodePudding user response:

pandas.DataFrame.to_sql returns the amount of rows affected automatically.

amount_of_rows = pd.to_sql(name, connection)

# Setting it to 0 if the database returned no rows.
if not amount_of_rows:
    amount_of_rows = 0

# Printing out the amount.
print(f"{amount_of_rows} rows were inserted.")

Will output 'X rows were inserted' if the query ran successfully.

P.S: Returns are not supported in versions prior to 1.4.0.

  • Related