Python SQLite更新语句报错:TypeError: function takes exactly 2 arguments (1 given)
Let's break down why you're hitting this TypeError: function takes exactly 2 arguments (1 given) error and how to fix it properly.
The Root Cause
The executemany() method requires two mandatory arguments:
- A SQL query string with placeholders (not hardcoded values)
- A sequence of parameter tuples (each tuple corresponds to a set of values for one execution of the query)
Your current code only passes the hardcoded SQL string as a single argument—missing the required second parameter sequence entirely. On top of that, directly concatenating variables like senNumStr and nameGroup into your SQL is a risky practice: it exposes you to SQL injection vulnerabilities and can break if your values contain special characters (like quotes).
Correct Usage Examples
Scenario 1: Updating multiple rows (what executemany() is designed for)
If you need to update multiple FormGroup entries with different SenNumber values, structure your parameters as a list of tuples and use ? placeholders in the query:
# Example: List of (SenNumber value, FormGroup identifier) tuples update_params = [ (123, "GroupA"), (456, "GroupB"), (789, "GroupC") ] # Use placeholders instead of string concatenation cur.executemany("UPDATE SEN_Table SET SenNumber = ? WHERE FormGroup = ?", update_params) # Don't forget to commit the transaction to save changes! conn.commit()
Scenario 2: Updating a single row
If you're only updating one FormGroup, use execute() instead of executemany()—it's more appropriate for single operations:
# Pass parameters as a single tuple cur.execute("UPDATE SEN_Table SET SenNumber = ? WHERE FormGroup = ?", (senNumStr, nameGroup)) conn.commit()
Key Takeaways
- Always use parameterized queries (with
?placeholders) instead of string concatenation to avoid SQL injection and syntax errors. executemany()strictly requires two arguments: the parameterized SQL string, and a sequence of parameter tuples.- Remember to call
conn.commit()after executing update statements—otherwise, your changes won't be saved to the database.
内容的提问来源于stack exchange,提问作者Adam Azam

