Search code examples
python-polarsrust-polars

Polars DataFrame save to sql


Is there a way to save Polars DataFrame into a database, MS SQL for example?

ConnectorX library doesn’t seem to have that option.


Solution

  • Polars doesen't support direct writing to a database. You can proceed in two ways:

    1. Export the DataFrame in an intermediate format (such as .csv using .write_csv()), then import it into the database.
    2. Process it in memory: you can convert the DataFrame in a simpler data structure using .to_dicts(). The result will be a list of dictionaries, each of them containing a row in key/value format. At this point is easy to insert them into a database using SqlAlchemy or any specific library for your database of choice.