I want to store some data from dataframes into an sqlite database. I am doing the following:
first create a database:
conn = sqlite3.connect("mydatabase.db")
then add table from dataframe df to database:
df.to_sql(table_name, conn,if_exists='append')
then querying the table from the database:
cur = conn.cursor()
cur.execute("SELECT * FROM table_name")
rows = cur.fetchall()
df2=pd.DataFrame(rows)
df2 does not have the original common names but has been reindex to 0, 1,2,3 etc.
How can I preserve the columns of the original dataframe?
Thank you
print(df.columns.tolist())before you write to SQLite?