Performance: SQL injection prevention
This concept affects page load speed indirectly by preventing slow database queries and security risks that can degrade user experience.
from sqlalchemy import text user_input = request.args.get('id') query = text("SELECT * FROM users WHERE id = :id") result = db.engine.execute(query, {'id': user_input})
user_input = request.args.get('id') query = f"SELECT * FROM users WHERE id = {user_input}" result = db.engine.execute(query)
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Unsafe string concatenation in SQL | 0 (backend only) | 0 | 0 | [X] Bad |
| Parameterized queries with SQLAlchemy | 0 (backend only) | 0 | 0 | [OK] Good |
cursor.execute("SELECT * FROM users WHERE id = %s", (user_id,))
results = cursor.fetchall()
print(results)user_id = request.args.get('id')
cursor.execute("SELECT * FROM users WHERE id = %s" % user_id)
results = cursor.fetchall()users = [("alice", 25), ("bob", 30)]
cursor.executemany(??? , users) What should replace ????Pass the test and this pattern moves unknown