I am writing some simple Python code to extract data from a CSV file and send it to SQL Server database. But I am getting this 07002 COUNT field incorrect or syntax error. The CSV file has multiple values and if I send just a single row of data, it inserts fine. The issue is with multiple values within the CSV file. Any help is appreciated!
Here is my code:
import_data = []
for csvFile in glob.glob("*.csv"):
with open(csvFile, 'r') as f:
reader = csv.reader(f)<br>
next(reader, None)#This skips the headers.
import_data = [tuple(line) for line in csv.reader(f)]
Insert data from CSV file to SQL Server
cur.executemany("""INSERT INTO dbo.currentobservations(STID,NAME,ST,LAT,LON,YR,MO,DA,HR,MI,TAIR,TDEW,RELH,CHIL,HEAT,WDIR,WSPD,WMAX,PRES,TMAX,TMIN,RAIN) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""", import_data)
#Here is the error message I am getting:
Traceback (most recent call last):
File "E:\Python3\Programs\sqlservermesonetcsv.py", line 40, in <module>
cur.executemany("""INSERT INTO dbo.currentobservations(STID,NAME,ST,LAT,LON,YR,MO,DA,HR,MI,TAIR,TDEW,RELH,CHIL,HEAT,WDIR,WSPD,WMAX,PRES,TMAX,TMIN,RAIN) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""", import_data)
File "E:\Python3\lib\site-packages\pypyodbc.py", line 1671, in executemany
self.execute(query_string, params, many_mode = True)
File "E:\Python3\lib\site-packages\pypyodbc.py", line 1605, in execute
self.execdirect(query_string)
File "E:\Python3\lib\site-packages\pypyodbc.py", line 1631, in execdirect
check_success(self, ret)
File "E:\Python3\lib\site-packages\pypyodbc.py", line 986, in check_success
ctrl_err(SQL_HANDLE_STMT, ODBC_obj.stmt_h, ret, ODBC_obj.ansi)
File "E:\Python3\lib\site-packages\pypyodbc.py", line 966, in ctrl_err
raise DatabaseError(state,err_text)
pypyodbc.DatabaseError: ('07002', '[07002] [Microsoft][ODBC SQL Server Driver]COUNT field incorrect or syntax error')