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:
CREATE TABLE Equipment (id INTEGER PRIMARY KEY, name TEXT, price INTEGER, weight REAL, info TEXT, ammo_cap INTEGER, availability_west TEXT, availability_east TEXT);
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:
self.cursor.execute("SELECT weight FROM Equipment WHERE name = ?", [item])
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.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3