Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to open a SQLite database named 'mydb.db'.
React Native
import * as SQLite from 'expo-sqlite'; const db = SQLite.[1]('mydb.db');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using createDatabase instead of openDatabase causes an error because it does not exist.
Trying to use connectDatabase or initDatabase which are not functions in expo-sqlite.
✗ Incorrect
The function openDatabase is used to open or create a SQLite database in expo-sqlite.
2fill in blank
mediumComplete the code to execute a SQL query that creates a table named 'users' with columns 'id' and 'name'.
React Native
db.transaction(tx => {
tx.executeSql('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY NOT NULL, name TEXT);', [], [1]);
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing null instead of a function causes no feedback on success.
Using error callbacks in place of success callbacks.
✗ Incorrect
The third argument to executeSql is a success callback function. Here, a simple console log confirms table creation.
3fill in blank
hardFix the error in the code to insert a user with name 'Alice' into the 'users' table.
React Native
db.transaction(tx => {
tx.executeSql('INSERT INTO users (name) VALUES ([1]);', ['Alice']);
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the value directly in the query causes SQL injection risk and errors.
Using ':name' which is not supported by expo-sqlite.
✗ Incorrect
The SQL query must use a placeholder '?' for the value to be inserted safely using parameters.
4fill in blank
hardFill both blanks to query all users and log their names.
React Native
db.transaction(tx => {
tx.executeSql('SELECT * FROM users;', [], (tx, [1]) => {
for (let i = 0; i < [2].rows.length; i++) {
console.log([2].rows.item(i).name);
}
});
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently causes errors.
Trying to access rows on the transaction object instead of results.
✗ Incorrect
The second argument of the success callback is commonly named 'results' and contains the rows property with the query results.
5fill in blank
hardFill all three blanks to update a user's name by id and handle success and error.
React Native
db.transaction(tx => {
tx.executeSql('UPDATE users SET name = [1] WHERE id = [2];', ['Bob', 1], [3], error => console.error(error));
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around '?' makes it a string literal, not a placeholder.
Passing null instead of a success callback loses feedback on success.
✗ Incorrect
Use '?' placeholders for parameters in the SQL query. The third argument is the success callback function.