正在执行到SQL数据库的Python脚本:计数字段不正确。。错误

2024-06-25 23:53:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些执行到SQL服务器的python脚本。错误日志显示在这个sqlInsrt()函数中发生了一些事情。错误是:

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

有人能解释一下这里可能出了什么问题吗?我以前也遇到过类似的SQL错误,例如(#预期的参数,给定的参数)

功能如下

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()

Tags: ofinforsql参数string错误create