0
0
React Nativemobile~10 mins

SQLite with expo-sqlite in React Native - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AopenDatabase
BcreateDatabase
CconnectDatabase
DinitDatabase
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.
2fill in blank
medium

Complete 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'
A() => console.log('Table created')
Bnull
C() => alert('Error')
D() => console.error('Failed')
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.
3fill in blank
hard

Fix 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'
A'Alice'
B:name
C?
D"Alice"
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.
4fill in blank
hard

Fill 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'
Aresults
Bresponse
CresultSet
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently causes errors.
Trying to access rows on the transaction object instead of results.
5fill in blank
hard

Fill 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'
A'?'
B?
C() => console.log('Update successful')
Dnull
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.