Challenge - 5 Problems
SQLite Master with expo-sqlite
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output after inserting and querying a record?
Consider this React Native code using expo-sqlite. What will be the value of
result.rows.length after the transaction?React Native
import * as SQLite from 'expo-sqlite'; const db = SQLite.openDatabase('test.db'); // Create table and insert one record db.transaction(tx => { tx.executeSql('CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY NOT NULL, name TEXT);'); tx.executeSql('INSERT INTO items (name) VALUES (?);', ['apple']); tx.executeSql('SELECT * FROM items;', [], (_, result) => { console.log(result.rows.length); }); });
Attempts:
2 left
💡 Hint
Remember that the SELECT query runs after the INSERT in the same transaction.
✗ Incorrect
The code creates a table if it doesn't exist, inserts one record, then queries all records. The SELECT callback receives the result with one row, so result.rows.length is 1.
❓ lifecycle
intermediate2:00remaining
When is the best time to open the SQLite database in a React Native app?
In a React Native app using expo-sqlite, when should you open the database connection to ensure it is ready for queries?
Attempts:
2 left
💡 Hint
Opening the database multiple times can cause issues and is unnecessary.
✗ Incorrect
Opening the database once at module load time ensures a single connection reused throughout the app. Opening inside render or on button press causes repeated openings and potential errors.
📝 Syntax
advanced2:00remaining
What error does this code produce?
What error will this code cause when running a transaction with expo-sqlite?
React Native
db.transaction(tx => {
tx.executeSql('INSERT INTO items (name) VALUES (?);', ['banana'],
() => console.log('Insert success'),
() => { console.log('Insert failure'); return false; }
);
});Attempts:
2 left
💡 Hint
The failure callback must return false to signal error handled.
✗ Incorrect
The failure callback must return false to prevent the transaction from rolling back. Returning undefined causes a TypeError internally in expo-sqlite.
advanced
2:00remaining
How to ensure data is loaded before navigating?
You want to load data from SQLite before navigating to a new screen. Which approach ensures data is ready before navigation?
Attempts:
2 left
💡 Hint
SQLite queries are asynchronous; navigation must wait for completion.
✗ Incorrect
The executeSql success callback runs after the query finishes. Navigating inside it ensures data is loaded before moving screens.
🧠 Conceptual
expert2:00remaining
Why use transactions in expo-sqlite?
Which is the main reason to use
db.transaction when working with expo-sqlite?Attempts:
2 left
💡 Hint
Think about what happens if one statement fails inside a transaction.
✗ Incorrect
Transactions ensure all statements succeed together or none apply, preserving data integrity. They do not speed up queries, cache results, or encrypt data.