MySQLdb allows dicts as query parameters. This answer shows all sorts of ways to do this. You only need to ensure privacy as such a parameter (tuple, dict ...) as the second parameter to "execute". DO NOT format your query as only one parameter to the execute method, otherwise you will probably encounter SQL injection attacks. Cm:
"SELECT * FROM users WHERE username = '%s'" % (user)
Think about what happens if:
user = "peter;DROP TABLE users" :_(
Another way is protected, as it allows the MySQLdb library to handle the necessary validation.
I donβt know what is wrong, because your request is fine for me:
# Connect to db # Open a cursor stmt = "SELECT * FROM users WHERE username = %(user)s" cursor.execute(stmt, {"user": "bob"}) user = cursor.fetchone() print user {'username': 'bob', 'alias': 'bobby', 'avatar': 'default', 'fullname': 'bob'}
Can you give us more information?
source share