Challenge - 5 Problems
RSQLite Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this R code connecting to an SQLite database?
Consider the following R code that creates an in-memory SQLite database, creates a table, inserts data, and queries it. What will be the output printed?
R Programming
library(DBI) con <- dbConnect(RSQLite::SQLite(), ":memory:") dbExecute(con, "CREATE TABLE fruits (name TEXT, quantity INTEGER)") dbExecute(con, "INSERT INTO fruits VALUES ('apple', 5), ('banana', 3), ('orange', 7)") res <- dbGetQuery(con, "SELECT name FROM fruits WHERE quantity > 4 ORDER BY quantity DESC") print(res) dbDisconnect(con)
Attempts:
2 left
💡 Hint
Look at the WHERE clause filtering quantity > 4 and the ORDER BY quantity DESC.
✗ Incorrect
The query selects fruits with quantity greater than 4, which are 'apple' (5) and 'orange' (7). Ordering by quantity descending puts 'orange' first, then 'apple'.
❓ Predict Output
intermediate1:30remaining
What error does this R code produce when connecting to a non-existent SQLite file?
What error message will this R code produce when trying to connect to a SQLite database file that does not exist?
R Programming
library(DBI)
con <- dbConnect(RSQLite::SQLite(), "nonexistent_file.sqlite")
dbDisconnect(con)Attempts:
2 left
💡 Hint
RSQLite creates the file if it does not exist when connecting.
✗ Incorrect
RSQLite automatically creates the SQLite file if it does not exist when connecting, so no error occurs.
🔧 Debug
advanced2:30remaining
Why does this R code fail to insert data into the SQLite table?
This R code tries to insert data into a SQLite table but fails with an error. Identify the cause.
R Programming
library(DBI) con <- dbConnect(RSQLite::SQLite(), ":memory:") dbExecute(con, "CREATE TABLE cars (model TEXT, year INTEGER)") data <- data.frame(model = c('Ford', 'Toyota'), year = c(2010, 2015)) dbWriteTable(con, "cars", data, append = TRUE) dbDisconnect(con)
Attempts:
2 left
💡 Hint
Check the usage of dbWriteTable with append=TRUE on an existing table.
✗ Incorrect
dbWriteTable with append=TRUE correctly inserts data into an existing table if column names match. Here, columns match and no error occurs.
❓ Predict Output
advanced2:00remaining
What is the output of this R code that lists tables in an SQLite database?
Given this R code that creates two tables in an SQLite database, what will dbListTables(con) return?
R Programming
library(DBI) con <- dbConnect(RSQLite::SQLite(), ":memory:") dbExecute(con, "CREATE TABLE users (id INTEGER, name TEXT)") dbExecute(con, "CREATE TABLE orders (order_id INTEGER, user_id INTEGER)") tables <- dbListTables(con) print(sort(tables)) dbDisconnect(con)
Attempts:
2 left
💡 Hint
dbListTables returns all user tables; sorting alphabetically helps.
✗ Incorrect
The database has two tables: 'users' and 'orders'. Sorting alphabetically returns 'orders' then 'users'.
🧠 Conceptual
expert3:00remaining
Which option correctly explains the behavior of dbDisconnect() in RSQLite?
When using DBI with RSQLite, what happens if you do not call dbDisconnect(con) after finishing database operations?
Attempts:
2 left
💡 Hint
Think about resource management and file locks in database connections.
✗ Incorrect
Not calling dbDisconnect() leaves the connection open, which can lock the database file and waste resources until R session ends or garbage collection occurs.