Search code examples
pythonmysqlpymssqlexecutemany

In sql.executemany(... syntax error near '('


I'm trying to execute the following code in python, but it results in syntax error near '(' error for executemany(..). When I remove te names from sql and just write %s it also results in the error that there are more placeholders thant the variables does anyone know how I can fix it?

upInfo ={"aa": "aaa","bb": "bbb","cc": "ccc"}
sql = 'UPDATE table SET a=  %(aa)s WHERE b= %(bb)s and c= %(cc)s'
con = pymssql.connect(...)
con.autocommit(True)
cur = con.cursor()
cur.executemany(sql, upInfo)

Solution

  • Since this is executemany(), it should be a list of dictionaries:

    upInfo = [{"aa": "aaa", "bb": "bbb", "cc": "ccc"}]
    

    Or, use a regular execute():

    cur.execute(sql, upInfo)