0
0
R Programmingprogramming~20 mins

Database connections (DBI, RSQLite) in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
RSQLite Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
    name
1   apple
2  orange
B
    name
1  banana
2   apple
C
    name
1  orange
2   apple
D
    name
1  banana
2  orange
Attempts:
2 left
💡 Hint
Look at the WHERE clause filtering quantity > 4 and the ORDER BY quantity DESC.
Predict Output
intermediate
1: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)
AError: unable to open database file
BWarning: database file missing
CError: file not found
DNo error, connection is created and file is created automatically
Attempts:
2 left
💡 Hint
RSQLite creates the file if it does not exist when connecting.
🔧 Debug
advanced
2: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)
AError because data frame column names do not match table column names
BNo error, data is inserted successfully
CError because dbWriteTable cannot append to an existing table without overwrite=FALSE
DError because dbWriteTable requires row names argument
Attempts:
2 left
💡 Hint
Check the usage of dbWriteTable with append=TRUE on an existing table.
Predict Output
advanced
2: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)
A[1] "orders" "users"
B[1] "users" "orders"
C[1] "sqlite_sequence" "users" "orders"
D[1] "orders"
Attempts:
2 left
💡 Hint
dbListTables returns all user tables; sorting alphabetically helps.
🧠 Conceptual
expert
3: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?
AThe connection remains open, potentially locking the database file and causing resource leaks
BThe connection is automatically closed when the R session ends, so no issues occur
CThe connection is closed immediately after the last query, so dbDisconnect() is optional
DThe database file is deleted automatically if dbDisconnect() is not called
Attempts:
2 left
💡 Hint
Think about resource management and file locks in database connections.