Using Code found on this site we are able to load data from a CSV to MSSQL where col headers = firstname or first_name. However if the col header contains a hyphen such as first-name then it fails.
Portion of code:
with open(yourcsv) as csvfile:
csvFile = csv.reader(csvfile, delimiter=',')
header = next(csvFile)
headers = map((lambda x: x.strip()), header)
insert = 'INSERT INTO {} ('.format(table) + ', '.join(headers) + ') VALUES '
for row in csvFile:
values = map((lambda x: "'"+x.strip()+"'"), row)
cursor.execute(insert +'('+ ', '.join(values) +');')
conn.commit() #must commit unless your sql database auto-commits
Error = cursor.execute(insert +'('+ ', '.join(values) +');') pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '-'. (102) (SQLExecDirectW)")
We need to be able to consume files which can contain a hyphen in the col name.