简体   繁体   中英

Python script executing to SQL databse: COUNT field incorrect.. ERROR

I have some python script that executes to a SQL server. Error log says that something is happening in this, sqlInsrt() function. The error is:

pypyodbc.DatabaseError: ('07002', '[07002] [Microsoft][ODBC SQL Server Driver]COUNT field incorrect or syntax error') 

Can anyone explain what is possibly going wrong here? I've had SQL errors before that are perhaps similar to this ("# of parameters expected, # of parameters given) for example

The function is below

def sqlInsrt(headers, values):
        #create string input of mylisth
        strheaders = ','.join(str(i) for i in headers)

        #create string input of %s corresponding to number of entries in mylisth
        placestr = ','.join(str(i) for i in ['?' for i in headers])

        #Setup and execute SQL query 
        replacestr = ', '.join(['{}=?'.format(h) for h in headers])
        insert = ("INSERT INTO capacitors ({strheaders}) VALUES ({placestr}) \
                  ON DUPLICATE KEY UPDATE {replacestr}").format(
                    strheaders=strheaders, placestr=placestr, replacestr=replacestr)
        cursor.execute(insert, values*2)
        cnx.commit()

Does strheaders and placestr have same length? It looks like you're requiring wrong column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM