Skip to content

Instantly share code, notes, and snippets.

@anna-anisienia
Created October 27, 2020 21:17
Show Gist options
  • Save anna-anisienia/7428c05859c62fb4bbcf752cbd367f63 to your computer and use it in GitHub Desktop.
Save anna-anisienia/7428c05859c62fb4bbcf752cbd367f63 to your computer and use it in GitHub Desktop.
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