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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-11-18 11:00:00 +0000
Seen: 11 times
Last updated: Jan 07 '22
How to analyze SQL using Python?
How can the SQL debug mode be activated in PostgreSQL version 15.2?
What is the procedure for using pg_restore on Windows with Docker?
What is the equivalent of SQL Server NVARCHAR in PostgreSQL?
What is the method to retrieve the JSON data from a column in SQL?
How to solve the issue of being unable to establish a connection with libpq on Mac M1?
Is it possible to launch multiple instances of PostgreSQL using Spring Boot Testcontainers?
Due to SyntaxError, why am I unable to create a TIMESTAMP WITH TIMEZONE column in postgres?