📖 Scenario: You are building a simple Node.js app that fetches user data from a database. To keep the app safe from harmful attacks, you need to use query parameterization.This means you will write your database query so that user input is handled safely, preventing attackers from changing your query in bad ways.
🎯 Goal: Build a Node.js script that safely queries a database using parameterized queries to get user information by ID.
📋 What You'll Learn
Create a variable with a user ID value
Create a SQL query string with a placeholder for the user ID
Use a parameterized query method to safely insert the user ID into the query
Complete the database query call with the correct parameters
💡 Why This Matters
🌍 Real World
Using parameterized queries is a key practice to protect web applications from SQL injection attacks, which are common security threats.
💼 Career
Backend developers and full-stack developers must know how to safely query databases to protect user data and maintain application security.
Progress0 / 4 steps
1
Set up the user ID variable
Create a variable called userId and set it to the number 42.
Node.js
Hint
Use const userId = 42; to create the variable.
2
Write the SQL query string with a placeholder
Create a variable called sqlQuery and set it to the string "SELECT * FROM users WHERE id = ?". This string uses ? as a placeholder for the user ID.
Node.js
Hint
Use a string with a question mark ? where the user ID will go.
3
Use parameterized query method to insert user ID safely
Write a line that calls db.query with sqlQuery and an array containing userId as parameters. Use the form db.query(sqlQuery, [userId]).
Node.js
Hint
Pass the SQL string and an array with userId to db.query.
4
Complete the database query call with a callback function
Add a callback function as the third argument to db.query that takes error and results parameters. Inside the callback, write an if statement that checks if error exists, and if so, returns the error. Otherwise, it logs results.
Node.js
Hint
Use a callback with (error, results) => { ... } and handle error and results inside.
Practice
(1/5)
1. Why is query parameterization important in Node.js database queries?
easy
A. It prevents SQL injection by separating user input from the query structure.
B. It makes queries run faster by caching results automatically.
C. It allows queries to be written without any syntax rules.
D. It automatically encrypts the database connection.
Solution
Step 1: Understand SQL injection risk
When user input is directly included in query strings, attackers can inject harmful SQL code.
Step 2: Role of parameterization
Parameterization sends user input separately, so the database treats it only as data, not code.
Final Answer:
It prevents SQL injection by separating user input from the query structure. -> Option A
Assuming injection will run despite parameterization
Expecting syntax errors from malicious input
Thinking all rows will be returned without a WHERE clause
4. Identify the error in this Node.js code using 'sqlite3' for parameterized queries:
db.run('INSERT INTO users (name, age) VALUES (?, ?)', name, age, (err) => {
if (err) throw err;
console.log('User added');
});
medium
A. The 'db.run' method cannot accept parameters.
B. Parameters should be passed as an array, not separate arguments.
C. The query string should use named parameters like ':name' instead of '?'.
D. The callback function is missing an error parameter.
Solution
Step 1: Check parameter passing syntax
sqlite3 expects parameters as an array or object, not separate arguments.
Step 2: Correct usage
Parameters should be passed as an array: [name, age].
Final Answer:
Parameters should be passed as an array, not separate arguments. -> Option B
Quick Check:
sqlite3 parameters = array [OK]
Hint: Pass parameters as array in sqlite3 queries [OK]
Common Mistakes:
Passing parameters as separate arguments
Confusing named and positional parameters
Ignoring callback error handling
5. You want to safely insert multiple user records in a batch using Node.js and 'pg' library. Which approach correctly uses query parameterization to avoid SQL injection?
hard
A. Run separate queries for each user without parameterization.
B. Build a single query string by concatenating all user values directly.
C. Use a parameterized query with multiple placeholders and pass all values in an array.
D. Use template literals to insert user data inside the query string.
Solution
Step 1: Understand batch insertion with parameterization
To insert multiple records safely, use placeholders for each value and pass all values as an array.
Step 2: Avoid unsafe concatenation or unparameterized queries
Concatenating strings or using template literals risks injection; separate queries without parameters are inefficient and unsafe.
Final Answer:
Use a parameterized query with multiple placeholders and pass all values in an array. -> Option C