Skip to content

Instantly share code, notes, and snippets.

@kmatt
Forked from bthaman/df_to_sql_fast.py
Last active November 19, 2024 02:05
Show Gist options
  • Save kmatt/601a0d97d0632066ba48263e45102cc9 to your computer and use it in GitHub Desktop.
Save kmatt/601a0d97d0632066ba48263e45102cc9 to your computer and use it in GitHub Desktop.
Polars dataframe to SQL Server using pyodbc, without Pandas or SQLAlchemy dependencies
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