So I'm trying to get a CSV file into an SQL Server,
sample csv file
STUFF,NAME,A DATE,A TIME,ANOTHER DATE,ANOTHER TIME,A NUMBER Bananas
John Smith,2019-11-20,17:00,2019-11-20,20:00,3 Apples,Jane Doe,2019-11-07,17:00,2019-11-07,23:00,6
here's how I'm trying to do it (based on this):
import csv
import pyodbc
thecsv = 'iamacsvfile.csv'
print('connecting')
drivr = "SQL Server"
servr = "1.2.3.4"
db = "testdata"
username = "user"
password = "thepassword"
my_cnxn = pyodbc.connect('DRIVER={};SERVER={};DATABASE={};UID={};PWD={}'.format(drivr,servr,db,username,password))
my_cursor = my_cnxn.cursor()
def insert_records(table, thecsv, my_cursor, my_cnxn):
with open(thecsv) 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 ({})' .format(', '.join(len(headers) * '?'))
for row in csvFile:
values = map((lambda x: x.strip()), row)
my_cursor.execute(insert, values)
my_cnxn.commit()
table = 'dbo.iamthetable'
mycsv = thecsv
insert_records(table, mycsv, my_cursor, my_cnxn)
my_cursor.close()
Error message:
insert = 'INSERT INTO {} ('.format(table) + ', '.join(headers) + ') VALUES ({})' .format(', '.join(len(headers) * '?'))
TypeError: object of type 'map' has no len()
I've seen some similar examples of this type of error (such as here) but I'm not sure how the solutions apply to this particular usage case. Can anyone assist?
(by the way if this entire code block is bad I'm open to a completely different method of doing the same, but haven't found anything that works yet)