Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the mysql2 package.
Node.js
const mysql = require('[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mysql' instead of 'mysql2' in require statement.
Using unrelated package names like 'pg' or 'mongodb'.
✗ Incorrect
The mysql2 package is imported using require('mysql2').
2fill in blank
mediumComplete the code to create a connection using mysql2.
Node.js
const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '[1]', database: 'testdb' }); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the username or host in the password field.
Leaving the password empty or incorrect.
✗ Incorrect
The password field should be the actual password string, e.g., 'password123'.
3fill in blank
hardFix the error in the code to connect to the database.
Node.js
connection.[1](err => { if (err) throw err; console.log('Connected!'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like 'connectTo' or 'open'.
Not calling the connect method at all.
✗ Incorrect
The correct method to establish the connection is 'connect'.
4fill in blank
hardFill both blanks to query the database and handle the result.
Node.js
connection.query('SELECT * FROM users WHERE id = ?', [[1]], (err, [2]) => { if (err) throw err; console.log(results); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'error' or 'data' instead of 'results' for the callback parameter.
Using a wrong variable name for the query parameter.
✗ Incorrect
The first blank is the variable holding the user id to query. The second blank is the variable holding the query results.
5fill in blank
hardFill all three blanks to close the connection after the query.
Node.js
connection.query('SELECT * FROM products', (err, results) => { if (err) throw err; console.log(results); connection.[1](() => { console.log('[2]'); process.[3](); }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'close' instead of 'end' to close the connection.
Not calling process.exit() to end the Node.js process.
✗ Incorrect
Use connection.end() to close the connection, log 'Connection closed', then call process.exit() to end the program.