Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!!

like image 683
Val10 Avatar asked Nov 07 '18 12:11

Val10


1 Answers

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:

  • SQLite engine configuration in SQLAlchemy

  • DataFrame.to_sql

like image 60
mremes Avatar answered Sep 22 '22 04:09

mremes