Pandas: to_sql return inserted row count or list

Created on 29 Nov 2018  路  2Comments  路  Source: pandas-dev/pandas

Potential feature, could df.to_sql() return the indexes of the inserted rows? Something like knex.js Query Builder

inserted = df.to_sql(con=engine, name=example_table, if_exists='replace', index=False)

Problem description

This would allow for more quickly knowing how many rows have been entered from your dataframe

Expected Output

Something like inserted = 2 if 2 rows have been added. Even better would be inserted = [24, 25] where inserted is now a list of the inserted primary keys

API Design Enhancement IO SQL

Most helpful comment

Is there any development towards this?

All 2 comments

Would seem like we could return the sqlalchemy ResultProxy - answers those type of questions

from sqlalchemy import create_engine
eng = create_engine('sqlite://')
eng.execute("CREATE TABLE data (t TEXT)")

r = eng.execute("INSERT INTO data VALUES ('jane'), ('jon'), ('jack');")

r
# Out[72]: <sqlalchemy.engine.result.ResultProxy at 0x15a83128>
r.rowcount
# Out[73]: 3

Is there any development towards this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

venuktan picture venuktan  路  3Comments

MatzeB picture MatzeB  路  3Comments

andreas-thomik picture andreas-thomik  路  3Comments

amelio-vazquez-reina picture amelio-vazquez-reina  路  3Comments

swails picture swails  路  3Comments