[Solved] Build a Parameterized SQL Statement from an array of values


I’m not sure what you expect this to do:

",".join(u"%s", (v,) for v in values)

but the error message gives you some clue on what you’re doing wrong:

SyntaxError: Generator expression must be parenthesized if not sole argument

the “Generator expression” part here being (v,) for v in values.

What you want is obviously something like:

values = ["a", "b", "c", "d"]
placeholders = ", ".join("%s" for _ in values) 
sql = "INSERT INTO table VALUES ({})".format(placeholders)
cursor.execute(sql, values)

solved Build a Parameterized SQL Statement from an array of values