Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save klitra/f6b87aa887daaa31d58a1e38ee39711e to your computer and use it in GitHub Desktop.
Save klitra/f6b87aa887daaa31d58a1e38ee39711e 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'
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()
@klitra
Copy link
Author

klitra commented Jul 17, 2022

Add, df columns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment