Created
October 27, 2020 21:18
-
-
Save anna-anisienia/e049072f933825de372516dda25d1735 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
Thanks for the example code!