10

I am trying to use Django's db connection variable to insert a pandas dataframe to Postgres database. The code I use is

df.to_sql('forecast',connection,if_exists='append',index=False)

And I get the following error

Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': relation "sqlite_master" does not exist LINE 1: SELECT name FROM sqlite_master WHERE type='table' AND name=?...

I think this happens because the Django connection object is not an sqlalchemy object and therefor Pandas assumes I am using sqlite. Is there any way to use .to_sql other than make another connection to the database?

2
  • What is you connection string? Commented May 23, 2020 at 18:25
  • django.db.connection would have worked, auto-selecting the default connection setting. Commented Jan 30, 2023 at 12:49

1 Answer 1

4

It is possible to create db configuration in setting.py file

DATABASES = {
    'default': env.db('DATABASE_URL_DEFAULT'),
    'other': env.db('DATABASE_URL_OTHER')
}
DB_URI_DEFAULT=env.str('DATABASE_URL_DEFAULT')
DB_URI_OTHER=env.str('DATABASE_URL_OTHER')

If you want to create sql_alchemy connection you should use DB_URI_DEFAULT or DB_URI_OTHER

in the init method of the class you will use .to_sql method you should write

from you_project_app import settings
from sqlalchemy import create_engine
import pandas as pd

class Example:

 def __init__(self):
        self.conn_default = create_engine(settings.DB_URI_DEFAULT).connect()

And when you use .to_sql method of pandas it should be like this:

df_insert.to_sql(table, if_exists='append',index=False,con=self.conn_default)
Sign up to request clarification or add additional context in comments.

1 Comment

Reference for how to convert django database settings into sqlalchemy connection strings: docs.sqlalchemy.org/en/14/core/engines.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.