1

The pandas package have a method called .to_sql that help to insert the current data frame on to the database.

.to_sql doc:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html

The second parameter is con

sqlalchemy.engine.(Engine or Connection) or sqlite3.Connection

Is it possible to generate the SQL query without passing a database connection?

2

1 Answer 1

1

We actually cannot print the query without a database connection, but we can use sqlalchemy create_mock_engine method and pass "memory" as the database URI to trick pandas, e.g:

from sqlalchemy import create_mock_engine, Metadata

def dump(sql, *multiparams, **params):
    print(sql.compile(dialect=engine.dialect))

engine = create_mock_engine("sqlite://:memory:", echo=True)
Metadata.create_all(engine, checkfirst=False)

frame.to_sql(engine)
Sign up to request clarification or add additional context in comments.

Comments

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.