-
-
Save kmatt/601a0d97d0632066ba48263e45102cc9 to your computer and use it in GitHub Desktop.
Polars dataframe to SQL Server using pyodbc, without Pandas or SQLAlchemy dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pl_to_sql(df, table, replace=False): | |
""" | |
to_sql() without Pandas | |
Appends or overwrites a SQL Server table using data from a DataFrame | |
Parameters: | |
df (DataFrame): df used to create/append table | |
table (str): Name of existing SQL Server table | |
replace (bool): Truncate before insert | |
Returns: | |
Nothing | |
""" | |
try: | |
cs = f'DRIVER={{ODBC Driver 18 for SQL Server}};SERVER={SERVER};DATABASE={DB};UID={UID};PWD={PWD}' | |
cols = [f'[{c}]' for c in df.columns] | |
cols = ','.join(cols) | |
vals = '?' * len(df.columns) | |
vals = ','.join(vals) | |
sql = 'INSERT INTO ' + table + ' (' + cols + ') VALUES (' + vals + ')' | |
with pyodbc.connect(cs) as conn: | |
cursor = conn.cursor() | |
cursor.fast_executemany = True | |
if replace: | |
cursor.execute(f"TRUNCATE TABLE {table}") | |
cursor.executemany(sql, df.rows(named=False)) | |
conn.commit() | |
cursor.close() | |
except Exception: | |
logger.opt(exception=True).error("cursor.executemany") | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment