Ask Your Question
4

How can the pandas dataframe be saved to SQL when df.to_sql is not working?

asked 2022-11-18 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-01-07 11:00:00 +0000

lakamha gravatar image

There are a few possible reasons why df.to_sql may not be working. In such cases, you can try the following alternatives:

  1. Use SQLAlchemy: Instead of using the standard Pandas function, use the SQLAlchemy library to connect to your database and write to it. This can provide more flexibility and better error handling. Here's an example of how to use SQLAlchemy:
from sqlalchemy import create_engine

engine = create_engine('postgresql://user:password@host:port/database')

df.to_sql('tablename', engine)
  1. Use a cursor to insert the rows: If you have a small dataset, you can use a cursor object to insert the rows one by one. Here's an example:
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()
  1. Export the data to a CSV file and use an SQL command to load it: If none of the above options work, you can export the data to a CSV file and use an SQL command to load it into your database. Here's an example:
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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2022-11-18 11:00:00 +0000

Seen: 11 times

Last updated: Jan 07 '22