Polars for Python - Connect to SQL Server (followup)


In a previous post, I took a brief look at a newer Python library called Polars. I stated that Polars does not support Microsoft SQL Server. Having looked into it more, I have found a package called polars-mssql that allows you to connect to SQL Server to directly import data into a Polars DataFrame and export data back to SQL Server. SQLAlchemy is required to be installed as well, although you don't need to import it into your code.

Here is a very brief code snippet that illustrates the process:

from polars_mssql import Connection

conn=Connection(
    server = "10.0.0.7",
    username="sa",
    password="wraFAPe3wraFAPe3",
    database = "AdventureWorks2022",
    driver = 'ODBC Driver 17 for SQL Server'
)

query = "SELECT * FROM Production.Location"
df = conn.read_query(query)

print (df)

If you run this code without the specified driver line, it will work but you will get a warning that you are using the outdated 'SQL Server' driver. The contents of the data frame will display correctly.

Check the developers website at Github for more examples.



Comments