Home >Backend Development >Python Tutorial >How to Fix SQLite Parameter Substitution Error in Python When Querying a Database?
Fix for SQLite Parameter Substitution Error in Python
When iteratively querying a database for specific values based on a list, a common pitfall arises in the use of the "?" parameter substitution method. While intended to prevent SQL injections, this method can lead to errors if not used correctly.
In the scenario described, the following error occurs: "sqlite3.ProgrammingError: Incorrect number of bindings supplied." This indicates that the number of parameters supplied to the execute() method does not match the expected number.
The error stems from the initial creation of the database table, which has 8 columns and therefore requires 8 parameters in the corresponding insert or update queries. However, when using the "?" substitution method, the second parameter should be a sequence containing only the values to be bound, not a single string.
To resolve this issue, the execute() method should be called as follows:
<code class="python">self.cursor.execute("SELECT weight FROM Equipment WHERE name = ?", [item])</code>
By passing a list with a single element ([item]) as the second parameter, the correct number of bindings is supplied and the error is avoided. Additionally, using the suggested form aligns with the recommendations outlined in the Python library reference for sqlite3 Cursor Objects.
The above is the detailed content of How to Fix SQLite Parameter Substitution Error in Python When Querying a Database?. For more information, please follow other related articles on the PHP Chinese website!