Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To utilize the polars library in Python to read data from an MS SQL server, you can follow these steps:

  1. Install the required packages by running the following command in your terminal:
pip install pyodbc pandas polars
  1. Import the necessary libraries:
import polars as pl
import pandas as pd
import pyodbc
  1. Set up a connection to the MS SQL server:
server = '<server_name>.<domain_name>.com'
database = '<database_name>'
username = '<username>'
password = '<password>'

connection_string = f'DRIVER={{ODBC Driver 17 for SQL Server}};' \
                    f'SERVER={server};DATABASE={database};UID={username};PWD={password};'
cnxn = pyodbc.connect(connection_string, autocommit=True)
  1. Retrieve data from the MS SQL server into a Pandas DataFrame:
query = "SELECT * FROM <table_name>"
df = pd.read_sql(query, cnxn)
  1. Convert the Pandas DataFrame to a Polars DataFrame:
pl_df = pl.from_pandas(df)

Now, you can use all the Polars DataFrame methods to manipulate and analyze the data from the MS SQL server.