Side-by-Side: Five Ways People Try to Stop SQL Injection
SQL injection happens when text typed by a user ends up being run as part of your SQL. Beginners hear many different pieces of advice about stopping it. This page puts five common approaches side by side and says plainly which ones you can rely on.
The problem in two lines
# Dangerous: the user's text is glued into the SQL
query = "SELECT * FROM users WHERE name = '" + name + "'"
If someone types ' OR '1'='1 as their name, the query becomes one that matches every user. The fix is to keep the SQL and the user's text apart.
Option 1: placeholders (parameterised queries)
cursor.execute("SELECT * FROM users WHERE name = ?", (name,))
The ? is a placeholder. The SQL is sent with a gap, and the value is sent separately, so the database never treats it as code. Different drivers use ?, %s, $1 or :name, but the idea is the same.
Verdict: the main defence. Use it for every value that comes from outside your code.
Option 2: an ORM
ORMs such as Django's or SQLAlchemy build queries with placeholders for you.
Verdict: safe for normal use. The risk returns if you use the ORM's "raw SQL" features and glue strings together there.
Option 3: escaping special characters yourself
Some people try to make input safe by adding backslashes or doubling quotes.
Verdict: fragile. It is easy to miss a case, and rules differ between databases and settings. Leave escaping to the driver by using placeholders.
Option 4: stored procedures
A stored procedure is a small program saved inside the database. Calling it with parameters is safe in the same way placeholders are.
Verdict: safe only if the procedure itself does not build SQL by gluing strings. A procedure that does that is just as vulnerable.
Option 5: allow-lists for things that cannot be placeholders
Placeholders work for values, but not for table names, column names or sort direction. If a user chooses "sort by date or by name", map their choice to a fixed list:
allowed = {"date": "created_at", "name": "name"}
column = allowed.get(user_choice, "created_at")
query = f"SELECT * FROM posts ORDER BY {column}"
Verdict: the right tool for that specific job. Never put the user's raw text in those spots.
The comparison at a glance
| Approach | Stops injection through values? | Main risk |
|---|---|---|
| Placeholders | Yes | Forgetting to use them somewhere |
| ORM | Yes, normally | Raw SQL helpers with glued strings |
| Manual escaping | Not reliably | Missed cases |
| Stored procedures | Yes, if written carefully | Dynamic SQL inside the procedure |
| Allow-lists | For names and keywords | Allowing raw input by mistake |
A good beginner rule
"If a value comes from a user, a file or another program, it goes in a placeholder. If a name or keyword comes from a user, it goes through an allow-list." For hands-on practice, try breaking and fixing a tiny login form, and the OWASP project publishes a well-known cheat sheet on preventing SQL injection.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.