Feb-27-2020, 07:33 PM
Hello,
I'm experimenting with panda and sqlalchemy for the first time where I'm importing a CSV file into a SQL Server database.
Any feedback on script and timing/count would be great!
Thanks for you help!
I'm experimenting with panda and sqlalchemy for the first time where I'm importing a CSV file into a SQL Server database.
from sqlalchemy import create_engine, func import pandas as pd import urllib import pyodbc database = {'servername': 'local', 'database': 'AdventureWorks2012', 'driver': 'driver=SQL Server Native Client 11.0'} names = ['Title', 'FirstName', 'LastName'] readFile = "C:/Temp/aw2012_exp.csv" # create the connection engine = create_engine('mssql+pyodbc://' + database['servername'] + '/' + database['database'] + "?" + database['driver']) df = pd.read_csv(readFile, names = names, header = None, encoding = "utf-8") df.to_sql(name = 'Python', schema = 'Person', con = engine, if_exists = 'replace', index = False) #result = engine.execute('SELECT COUNT(*) FROM PERSON.PYTHON') #cnt = pd.read_sql('SELECT COUNT(*) FROM Person.Python', engine) #print(cnt) #result.fetchall()I'd like to be able to output the timing of the insert as well as the count of the records inserted. The 'print[cnt]' doesn't seem like the best way to handle this.
Any feedback on script and timing/count would be great!
Thanks for you help!