0
votes

I know this is a popular topic but I searched the various answers and didn't see a clear answer to my issue. I have a function that I want to use to insert records into my NDBC database that is giving me the error I mentioned in the title. The function is below:

def insertStdMet(station,cursor,data):
# This function takes in a station id, database cursor and an array of data.  At present
# it assumes the data is a pandas dataframe with the datetime value as the index
# It may eventually be modified to be more flexible.  With the parameters
# passed in, it goes row by row and builds an INSERT INTO SQL statement
# that assumes each row in the data array represents a new record to be
# added.
fields=list(data.columns) # if our table has been constructed properly, these column names should map to the fields in the data table
# Building the SQL string
strSQL1='REPLACE INTO std_met (station_id,date_time,'
strSQL2='VALUES ('
for f in fields:
    strSQL1+=f+','
    strSQL2+='%s,'
# trimming the last comma
strSQL1=strSQL1[:-1]
strSQL2=strSQL2[:-1]
strSQL1+=") " + strSQL2 + ")"
# Okay, now we have our SQL string.  Now we need to build the list of tuples
# that will be passed along with it to the .executemany() function.
tuplist=[]
for i in range(len(data)):
    r=data.iloc[i][:]
    datatup=(station,r.name)
    for f in r:
        datatup+=(f,)
    tuplist.append(datatup)
cursor.executemany(strSQL1,tuplist)

When we get to the cursor.executemany() call, strSQL looks like this:

REPLACE INTO std_met (station_id,date_time,WDIR,WSPD,GST,WVHT,DPD,APD,MWD,PRES,ATMP,WTMP,DEWP,VIS) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'

I'm using % signs throughout and I am passing a list of tuples (~2315 tuples). Every value being passed is either a string,datetime, or number. I still have not found the issue. Any insights anyone cares to pass along would be sincerely appreciated.

Thanks!

1

1 Answers

0
votes

You haven't given your SQL query a value for either station_id or date_time, so when it goes to unpack your arguments, there are two missing.

I suspect you want the final call to be something like:

REPLACE INTO std_met 
(station_id,date_time,WDIR,WSPD,GST,WVHT,DPD,APD,MWD,
 PRES,ATMP,WTMP,DEWP,VIS) VALUES (%s, %s, %s,%s,%s,%s,
                                  %s,%s,%s,%s,%s,%s,%s,%s)'

Note the extra two %s. It looks like your tuple already contains values for station_id and date_time, so you could try this change:

strSQL1='REPLACE INTO std_met (station_id,date_time,'
strSQL2='VALUES (%s, %s, '