Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize Knex with a SQLite3 client.
Express
const knex = require('knex')({ client: '[1]', connection: { filename: './data.db' } });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mysql' or 'pg' when the connection is a file path.
Forgetting to specify the client.
✗ Incorrect
Knex uses the client option to specify the database type. For SQLite3, use sqlite3.
2fill in blank
mediumComplete the code to select all rows from the 'users' table using Knex.
Express
knex('[1]').select('*').then(rows => console.log(rows));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong table name like 'items' or 'orders'.
Forgetting to call select('*').
✗ Incorrect
The table name to query is 'users'.
3fill in blank
hardFix the error in the Knex query to insert a new user with name 'Alice'.
Express
knex('[1]').insert({ name: 'Alice' }).then(() => console.log('Inserted'));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular 'user' instead of 'users'.
Using unrelated table names.
✗ Incorrect
The correct table name is 'users' for inserting user data.
4fill in blank
hardFill both blanks to update the email of user with id 5 to 'new@example.com'.
Express
knex('[1]').where('[2]', 5).update({ email: 'new@example.com' }).then(() => console.log('Updated'));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong table names like 'email' or 'username' as table.
Filtering by 'email' instead of 'id'.
✗ Incorrect
The table is 'users' and the column to filter by is 'id'.
5fill in blank
hardFill all three blanks to delete users older than 30 from the 'users' table.
Express
knex('[1]').where('[2]', '[3]', 30).del().then(() => console.log('Deleted'));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong table names.
Using '<' instead of '>' for age comparison.
✗ Incorrect
We delete from 'users' where 'age' is greater than 30.