Probably the main reason why to_sql
needs a SQLAlchemy Connectable
(Engine
or Connection
) object is that to_sql
needs to be able to create the database table if it does not exist or if it needs to be replaced. Early versions of pandas worked exclusively with DBAPI connections, but I suspect that when they were adding new features to to_sql
they found themselves writing a lot of database-specific code to work around the quirks of the various DDL implementations.
On realizing that they were duplicating a lot of logic that was already in SQLAlchemy they likely decided to “outsource’ all of that complexity to SQLAlchemy itself by simply accepting an Engine
/Connection
object and using SQLAlchemy’s (database-independent) SQL Expression language to create the table.
it makes me use two different frameworks to connect to my database
No, because .read_sql_query()
also accepts a SQLAlchemy Connectable
object so you can just use your SQLAlchemy connection for both reading and writing.