Reputation: 321
Im just trying to upload the table to company server however after first upload it doesn't neither append nor replace in same table.
#Import Libraries#
import pyodbc
import sqlalchemy as sal
from sqlalchemy import create_engine
import pandas as pd
#SQL Alchemy Connection String
engine = sal.create_engine('mssql+pyodbc://GRATHDB03\REPLICA/DATAWRHS-TRAINING?driver=SQL Server Native Client 11.0?Trusted_Connection=yes',
fast_executemany=True
)
#Read and Upload the file
df=pd.read_csv(r'C:\Users\ehan\Downloads\results-survey177217 (1).csv')
df.columns=df.columns.str.replace('/', '').str.replace('?','').str.replace('&','').str.replace(';','').str.replace(',','').str.replace(':','').str.replace('Kindly rate your trainer on the following catagories','')
df.columns=df.columns.str.replace('[','').str.replace(']','')
df['Date submitted']=pd.to_datetime(df['Date submitted'], dayfirst=True)
df['Date started']=pd.to_datetime(df['Date started'],dayfirst=True)
df.to_sql('limesurveytest3', engine, index=False, if_exists='append',schema='[dbo]')
and here is the error.
(pyodbc.ProgrammingError) ('42S01', "[42S01] [Microsoft][SQL Server Native Client 11.0][SQL Server]There is already an object named 'limesurveytest3' in the database. (2714) (SQLExecDirectW)")
Upvotes: 1
Views: 227
Reputation: 321
Worked without []
df.to_sql('limesurveytest3', engine, index=False, if_exists='append',schema='dbo')
Upvotes: 1