NullCharacter0 / Insert-Python-dataframe-into-SQL-table

Inserting Python dataframe into SQL table if the table is created

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Insert-Python-dataframe-into-SQL-table

Inserting Python dataframe into SQL table if the table is created

Requirements

import pandas as pd 
import sqlite3 

Function


for v in df.iterrows():
    conn.execute("INSERT OR REPLACE INTO CANDLE_STICK values (?,?,?,?,?,?)", [str(v[0])] + list(v[1].values))

conn.commit()
print(conn.total_changes)

Prior


conn = sqlite3.connect("CoinbaseAPI.sqlite")
         # Purpose is to create a table if it doesnt exist 
         df.to_sql(name="CANDLE_STICK",
                   con=conn,
                   if_exists="fail", # "append","replace","fail" default: fail 
                   index=True, #  Sets df index to table index
                   dtype={
                       "start": "TIMESTAMP PRIMARY KEY",
                       "open": "REAL",
                       "high": "REAL",
                       "low": "REAL",
                       "close": "REAL",
                          }
                   )

About

Inserting Python dataframe into SQL table if the table is created


Languages

Language:Python 100.0%