I'm trying to take a csv file and import it into sql server express using Python. I've googled the problem and the solution I'm seeing that seems to be working for everyone else is:
import pymssql
import csv
conn = pymssql.connect(
server="servername",
user='username',
password='password',
database='db'
)
cursor = conn.cursor()
with open('new_csv_file.csv', 'r') as f:
reader = csv.reader(f)
columns = next(reader)
query = 'insert into MyTable({0}) values ({1})'
query = query.format(','.join(columns), ','.join('?' * len(columns)))
cursor = conn.cursor()
for data in reader:
values = map((lambda x: x.strip()), data) # No need for the quote
print(tuple(values))
cursor.execute(query, tuple(values))
cursor.commit()
conn.commit()
cursor.close()
print("Done")
conn.close()
I've confirmed the code works it's just the "execute()" part doesn't. query is ok, but "values" is giving me the following error:
(102, b"Incorrect syntax near 'Year'.DB-Lib error message 20018, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n")
Where 'Year' is the column I'm trying to fit the data in.
Thanks.