Forked from anna-anisienia/pyodbc_sql_server_fast_executemany.py
Created
January 3, 2023 07:28
-
-
Save saryeHaddadi/14021af943c8bec3eb7994088561f50f to your computer and use it in GitHub Desktop.
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
import pyodbc | |
import pandas as pd | |
df = pd.read_csv('myfile.csv') | |
MY_TABLE = 'some_tbl' | |
conn = pyodbc.connect(driver='{ODBC Driver 17 for SQL Server}', | |
server='MYSERVER', | |
database='MYDB', | |
uid='MYUSER', pwd='MYPASSWORD') | |
insert_to_tmp_tbl_stmt = f"INSERT INTO {MY_TABLE} VALUES (?,?,?,?,?,?)" | |
cursor = conn.cursor() | |
cursor.fast_executemany = True | |
cursor.executemany(insert_to_tmp_tbl_stmt, df.values.tolist()) | |
print(f'{len(df)} rows inserted to the {MY_TABLE} table') | |
cursor.commit() | |
cursor.close() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment