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:
SQLite engine configuration in SQLAlchemy
DataFrame.to_sql
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With