0

I need to import multiple excel file data into an SQLite database, these excel files generated weekly basis having the same naming convention and data structure.

in my code 1st file data imported successfully, when I am trying to import the 2nd file it is giving me an error.

enter image description here

import sqlite3
import pandas as pd
filename="cps"
con=sqlite3.connect(filename+".db")
wb = pd.read_excel('CPS\cps29.xlsx',sheet_name = None)
for sheet in wb:

    wb[sheet].to_sql(sheet,con,index=False)
    con.commit()
    con.close()

I need to append data into my database.

1 Answer 1

1

Use if_exists='append' to insert new rows if the table already exists (and if the table does not exist, it will be created):

for sheet in wb:
    wb[sheet].to_sql(sheet, con, index=False, if_exists='append')
    con.commit()
con.close()

Also note that con.close() should not be called inside the for-loop. Call it once after the for-loop, after all the data has been committed.

Sign up to request clarification or add additional context in comments.

Comments

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.