Forked from anna-anisienia/pyodbc_sql_server_fast_executemany.py
Last active
July 17, 2022 03:34
-
-
Save klitra/f6b87aa887daaa31d58a1e38ee39711e to your computer and use it in GitHub Desktop.
This file contains 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' | |
MY_TABLE_C = df.colums | |
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}({MY_TABLE_C}) 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
Add, df columns