How to deal with an apostrophe while writing into a MySQL database
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Apostrophes (single quotes ') in SQL are string delimiters. Inserting text like O'Brien or it's directly into a query breaks the SQL syntax and can create SQL injection vulnerabilities. The correct solution is to use parameterized queries (prepared statements), which handle escaping automatically. Manual escaping with backslashes or doubled quotes is error-prone and should be avoided.
The Problem
Solution 1: Parameterized Queries (Recommended)
Always use parameterized queries. The database driver handles all escaping:
Python (mysql-connector / PyMySQL)
PHP (PDO)
Java (JDBC)
Node.js (mysql2)
C# (MySqlConnector)
Solution 2: Escape the Apostrophe in SQL
If you must write raw SQL (e.g., in MySQL CLI), double the single quote:
The doubled-quote approach ('') is the SQL standard. The backslash approach (\') is MySQL-specific and depends on the NO_BACKSLASH_ESCAPES SQL mode.
Solution 3: mysql_real_escape_string (PHP Legacy)
This is the legacy approach and is not recommended. It's error-prone (forget to call it once and you have an injection vulnerability). Use PDO prepared statements.
Why Not Just Escape Manually?
Parameterized queries separate the SQL structure from the data at the protocol level. The database engine never confuses data for SQL syntax, making injection impossible.
Handling Apostrophes in SELECT/WHERE
Common Pitfalls
- String concatenation in queries: Never build SQL by concatenating user input:
f"SELECT * FROM users WHERE name = '{user_input}'". This is the #1 cause of SQL injection vulnerabilities. NO_BACKSLASH_ESCAPESmode: If MySQL hasNO_BACKSLASH_ESCAPESenabled,\'does not work — only''(doubled quotes) is valid. Parameterized queries work regardless of SQL mode.- ORM safety: ORMs like SQLAlchemy, Django ORM, and ActiveRecord use parameterized queries internally. Direct
.filter(name="O'Brien")is safe. But raw SQL through the ORM (.raw()) needs manual parameterization. - Stored procedures: Even inside stored procedures, use prepared statements for dynamic SQL:
PREPARE stmt FROM ...; EXECUTE stmt USING @param; - Multi-byte character attacks: In some character sets, specially crafted multi-byte characters can bypass escaping functions. Parameterized queries are immune to this class of attacks.
Summary
- Always use parameterized queries (prepared statements) — they handle apostrophes and prevent SQL injection automatically
- Never concatenate user input into SQL strings
- In raw SQL (MySQL CLI), escape apostrophes by doubling them:
'O''Brien' - Every language has parameterized query support:
%s(Python),?(PHP/Java/Node),@param(C#) - Manual escaping functions (
mysql_real_escape_string,replace("'", "\\'")) are fragile and should not be used
Related reading
- How to deal with persistent storage (e.g. databases) in Docker
- How to deal with persistent storage e.g. databases in Docker
- How to debug Lock wait timeout exceeded on MySQL?
- How to declare a variable in MySQL?
- How to define a custom ORDER BY order in mySQL
- How to define a key to GET human readable ids in REDIS?
- How to delete a certain row from mysql table with same column values?
- How to delete a column from a table in MySQL

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.