Skip to content

Instantly share code, notes, and snippets.

@random82
Created February 6, 2019 03:23
Show Gist options
  • Save random82/04d0dc091066379e50c302222ad52571 to your computer and use it in GitHub Desktop.
Save random82/04d0dc091066379e50c302222ad52571 to your computer and use it in GitHub Desktop.
Import CSV to MSSQL with Pandas - Python 3 version
from sqlalchemy import create_engine
import urllib
import pyodbc
import pandas as pd
df = pd.read_csv("./data.csv")
quoted = urllib.parse.quote_plus("DRIVER={SQL Server Native Client 11.0};SERVER=(localDb)\ProjectsV14;DATABASE=database")
engine = create_engine('mssql+pyodbc:///?odbc_connect={}'.format(quoted))
df.to_sql('TargetTable', schema='dbo', con = engine)
result = engine.execute('SELECT COUNT(*) FROM [dbo].[TargetTable]')
result.fetchall()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment