1 | initial version |
There are a few possible reasons why df.to_sql may not be working. In such cases, you can try the following alternatives:
from sqlalchemy import create_engine
engine = create_engine('postgresql://user:password@host:port/database')
df.to_sql('tablename', engine)
import sqlite3
conn = sqlite3.connect('mydb.db')
cursor = conn.cursor()
for index, row in df.iterrows():
cursor.execute('INSERT INTO mytable VALUES (?,?,?,?,?)', tuple(row))
conn.commit()
df.to_csv('data.csv', index=False)
# SQL command to load the data
LOAD DATA LOCAL INFILE 'data.csv'
INTO TABLE mytable
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
Note that the syntax of the SQL command may vary depending on your database system.