Created
February 6, 2019 03:23
-
-
Save random82/04d0dc091066379e50c302222ad52571 to your computer and use it in GitHub Desktop.
Import CSV to MSSQL with Pandas - Python 3 version
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
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