简体   繁体   中英

Insert a pandas dataframe into a SQLite table

So I have a dataframe imported from excel and an SQL Table with matching columns. So far I have been updating the table using the columns as lists:

Schedule_Frame = DataFrame(Sheet_Schedule)

Column_ID=Schedule_Frame['ID']

Column_list=list(Column_ID)


for i in range(len(Column_list)):

miCursor.execute("UPDATE SCHEDULE SET ID=? WHERE rowid=?",(Column_list[i],i))

However, since what I have in SQLite is a table that matches my dataframe columns, I am sure that there is a way to update the whole SQLite table using my frame.

Any ideas how to do it?

Thanks a lot!!

I think you're using sqlite3 package to access your SQLite database. How about using SQLAlchemy – which operates well with Pandas' data structures – to access the database?

from sqlalchemy import create_engine
engine = create_engine('sqlite:///<replace_this_with_path_to_db_file>', echo=False)

Then doing:

Schedule_Frame.to_sql('SCHEDULE', con=engine, if_exists='append')

Edit : Example code

from sqlalchemy import create_engine
import pandas as pd

engine = sqlalchemy.create_engine('sqlite:///my.db', echo=False)
df = pd.DataFrame([[1,2],[1,2]], columns=['a', 'b'])

df.to_sql('mytable', con=engine, if_exists='append')

In sqlite3 CLI:

sqlite> select * from 'mytable';
0|1|2
1|1|2

Resources:

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