Product case study · November 27, 2026
Ask Claude Code to fix SQLite searches for % and _
Reproduce a bound SQLite query that returns too many rows, then ask Claude Code to preserve literal percent signs and underscores with fixed regression tests.

Binding a search value keeps it separate from SQL syntax, but does not stop LIKE interpreting its wildcard characters. Test the matching contract too.
A reader searches your document catalog for 100%. The results include 1000 plans. The database query uses a placeholder, so the agent says it is already fixed. The placeholder is doing its job; the search contract is still broken.
This exercise asks Claude Code to repair a literal-substring search without removing punctuation or changing the stored titles. You will give it tests that distinguish the intended result from a plausible-looking patch.
You need Bash, Python 3 with sqlite3, and an installed, signed-in Claude Code session. The Python baseline and reference repair were tested locally. The agent prompt is an authored exercise, not a recorded Claude session. Official sources were checked November 27, 2026.
Reproduce the extra results
Create a disposable folder and a small search module:
search_lab=$(mktemp -d /tmp/claude-literal-search.XXXXXX)
cd "$search_lab"
cat > catalog.py <<'PY'
def find_titles(db, term):
query = "SELECT id, title FROM documents WHERE title LIKE ? ORDER BY id"
return db.execute(query, ("%" + term + "%",)).fetchall()
PY
This query already binds the value separately, following Python’s sqlite3 parameter guidance. Binding prevents the value from becoming SQL query structure. It does not decide what the LIKE operator considers a match.
In SQLite, % matches any sequence of characters and _ matches one character. An explicit ESCAPE character lets a pattern represent those symbols literally. See the SQLite LIKE reference.
Our product decision: users type text, not a pattern language. Searching for % should find titles containing a percent sign.
Write down the contract
Save this as SPEC.md:
find_titles(db, term) receives a SQLite connection and a Python string.
Search for term anywhere in a document title, treating %, _ and ! literally.
Keep SQLite's default LIKE case behavior: ASCII case-insensitive matching.
Do not promise Unicode case folding. Do not change connection settings.
Return a list of (id, title) tuples ordered by numeric id.
Return [] for an empty term; do not trim nonempty terms.
Keep the SQL query structure fixed and bind the pattern as a value.
Do not mutate stored rows or the schema. Add no dependencies.
Only catalog.py may change. Preserve SPEC.md and the tests.
Non-string input and custom SQLite extensions are outside this exercise.
The empty-input rule is deliberate. A search box that has not received a term should not return the entire catalog. A different product could choose otherwise; decide before asking an agent to implement it.
Give the repair independent tests
Save this as test_catalog.py:
import sqlite3
import unittest
from catalog import find_titles
class SearchTests(unittest.TestCase):
def setUp(self):
self.db = sqlite3.connect(":memory:")
self.addCleanup(self.db.close)
self.db.execute(
"CREATE TABLE documents (id INTEGER PRIMARY KEY, title TEXT NOT NULL)"
)
self.rows = [
(1, "100% plans"), (2, "1000 plans"),
(3, "draft_v2"), (4, "draftXv2"),
(5, "Wow!_done"), (6, "Wow!Xdone"),
(7, "O'Neil notes"), (8, "100% plans"),
]
self.db.executemany("INSERT INTO documents VALUES (?, ?)", self.rows)
def tearDown(self):
self.assertEqual(
self.db.execute("SELECT id, title FROM documents ORDER BY id").fetchall(),
self.rows,
)
def test_percent_and_duplicates(self):
self.assertEqual(find_titles(self.db, "100%"), [self.rows[0], self.rows[7]])
def test_underscore(self):
self.assertEqual(find_titles(self.db, "draft_"), [self.rows[2]])
def test_escape_character_next_to_wildcard(self):
self.assertEqual(find_titles(self.db, "!_"), [self.rows[4]])
def test_ascii_case_and_substring(self):
self.assertEqual(find_titles(self.db, "NEIL"), [self.rows[6]])
def test_apostrophe_and_sql_shaped_text(self):
self.assertEqual(find_titles(self.db, "O'Neil"), [self.rows[6]])
self.assertEqual(find_titles(self.db, "' OR 1=1 --"), [])
def test_empty_term(self):
self.assertEqual(find_titles(self.db, ""), [])
def test_missing_term(self):
self.assertEqual(find_titles(self.db, "absent"), [])
def test_no_trimming(self):
self.assertEqual(find_titles(self.db, " plans "), [])
Run the baseline:
python3 -m unittest -v test_catalog
In our local run, four tests failed: percent, underscore, the !_ combination and empty input. Four passed. The apostrophe case already worked, which is useful evidence that “add parameter binding” is not the missing repair.
The fixture includes duplicate titles with different IDs, so returning only the first match is also wrong. Each test uses a fresh in-memory database; the final row comparison catches accidental edits to the catalog.
Ask Claude Code for a bounded repair
Start claude in the practice folder and paste:
Read SPEC.md, catalog.py and test_catalog.py.
Run python3 -m unittest -v test_catalog and report the observed failures.
Explain why the bound parameter still lets 100% match 1000 plans.
Change only catalog.py to meet SPEC.md and the unchanged tests.
Keep parameter binding. Explain how your pattern handles %, _ and !.
Do not install packages, access external databases, delegate, commit or publish.
Run the original tests again and report the actual result and remaining limits.
This follows Anthropic’s debugging guidance: supply the reproduction and narrow the change. For session setup and permission review, use the first Claude Code project lesson. A prompt specifies scope; it does not establish a filesystem sandbox.
Read the proposed patch before accepting it. Removing % from the input changes what the reader typed. Replacing LIKE with = removes substring matching. Fetching every row into Python adds work the brief did not request and can quietly change case handling.
Compare a reference repair
Replace catalog.py with this implementation if you want to inspect a known local result:
def find_titles(db, term):
if term == "":
return []
escaped = term.replace("!", "!!").replace("%", "!%").replace("_", "!_")
pattern = "%" + escaped + "%"
query = (
"SELECT id, title FROM documents "
"WHERE title LIKE ? ESCAPE '!' ORDER BY id"
)
return db.execute(query, (pattern,)).fetchall()
The escape character is !. Escape existing exclamation marks first, then the two wildcard characters, then add the outer wildcards needed for substring search. For example, the input !_ becomes !!!_ before wrapping: a literal exclamation mark followed by a literal underscore.
This is pattern escaping, not manual SQL quoting. The completed pattern still travels through the bound parameter. The SQL string stays constant.
All eight tests passed against this reference locally. Leave the agent session and rerun the same command yourself:
python3 -m unittest -v test_catalog
If punctuation tests still fail, inspect whether the query includes ESCAPE '!' and whether existing ! characters are escaped before new escape markers are inserted. If the empty test returns everything, inspect the early return. If imports fail, check that the terminal is in the folder containing both files.
If Claude changed expected results, restore the original tests before judging the repair. Keep the failing baseline and final output together so a later reviewer can see what changed.
Carry the right evidence into the real app
The fixture verifies matching semantics for this small contract. It does not measure performance on a production catalog or test your application’s access controls. Case handling also needs its own requirement: SQLite’s default LIKE folds ASCII case, but does not provide general Unicode case folding. Test the languages your readers actually use before promising that behavior.
For a real search box, add one regression case using the troublesome title shape and verify the displayed results through the application. Keep the in-memory tests as the fast check. A green helper test cannot tell you whether the interface passed the intended search string.
If your original query instead crashes on apostrophes, start with the parameter-binding tutorial. That fixes query construction. This lab covers the next question: whether a valid query finds the right rows.
Sources
Checked November 27, 2026:
- SQLite SQL expressions: LIKE — wildcard characters, explicit escaping and default case behavior.
- Python sqlite3 — passing values separately using placeholders.
- Claude Code common workflows — reproduction-led debugging and test verification.
Put this to work
Why a bound LIKE pattern can still match more rows than intended.
Try
Give Claude Code a literal-substring search contract and a failing fixture.
Prove it worked
Run eight unchanged tests against the baseline and reference repair.
Keep in view
- Reproduce a search that treats a typed percent sign as a wildcard.
- Keep parameter binding while repairing the matching behavior.
- Test punctuation, empty input and unchanged database contents.