"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > When Using SQLite3 in Python, Why \"Incorrect Number of Bindings Supplied\" When Utilizing \"?\" Parameter Substitution?

When Using SQLite3 in Python, Why \"Incorrect Number of Bindings Supplied\" When Utilizing \"?\" Parameter Substitution?

Published on 2024-11-07
Browse:964

When Using SQLite3 in Python, Why \

SQLite Parameter Substitution Conundrum

In an attempt to safeguard against SQL injections, a developer encountered an error while utilizing SQLite3 with Python 2.5. When employing the recommended "?" parameter substitution to prevent injections, they faced the following dilemma:

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 8 supplied.

This issue seemed to stem from the database's initial creation, which contained eight bindings. However, using the less secure "%s" substitution for each item name resolved the problem.

for item in self.inventory_names:
    self.cursor.execute("SELECT weight FROM Equipment WHERE name = '%s'" % item)
    self.cursor.close()

The solution to this perplexity lies in understanding that the Cursor.execute() method requires a sequence as its second parameter. In this instance, the developer was providing a string that happened to be eight characters long. To rectify this, the following code modification should be implemented:

self.cursor.execute("SELECT weight FROM Equipment WHERE name = ?", [item])

By conforming to this parameter specification, the issue can be effectively addressed, allowing for secure and efficient data retrieval from SQLite3.

Release Statement This article is reprinted at: 1729322955 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

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