Home >Backend Development >Python Tutorial >Is SQLite Parameter Substitution Causing Binding Errors in Python?
SQLite Parameters Substitution Issue
When utilizing SQLite3 with Python 2.5, a common issue arises when attempting to iterate through a list and retrieve data from a database. Using the suggested "?" parameter as a precaution for SQL injections often results in an error regarding the number of bindings.
Upon investigation, it becomes apparent that the error stems from the initial creation of the database table. The creation statement, such as:
<code class="sql">CREATE TABLE Equipment (id INTEGER PRIMARY KEY, name TEXT, price INTEGER, weight REAL, info TEXT, ammo_cap INTEGER, availability_west TEXT, availability_east TEXT);</code>
registers eight bindings, even though only one parameter is being used during the query.
To resolve this issue, modify the code to utilize a sequence as the second parameter to the Cursor.execute() method:
<code class="python">self.cursor.execute("SELECT weight FROM Equipment WHERE name = ?", [item])</code>
This modification ensures that a sequence is supplied to the method, resolving the confusion over the number of bindings.
Referencing the SQLite3 Cursor Objects documentation can provide further guidance on this topic.
The above is the detailed content of Is SQLite Parameter Substitution Causing Binding Errors in Python?. For more information, please follow other related articles on the PHP Chinese website!