eval_dbi <- FALSE if(Sys.getenv("GLOBAL_EVAL") != "") eval_dbi <- Sys.getenv("GLOBAL_EVAL")
library(DBI)
DBI
Connecting and adding data to a database
Load the DBI
package
r
library(DBI)
Use dbConnect()
to open a Database connection
r
{{dbi_connection}}
Use dbListTables()
to view existing tables, there should be 0 tables
r
dbListTables(con)
Use dbWriteTable()
to create a new table using mtcars
data. Name it db_mtcars
r
dbWriteTable(con, "db_mtcars", mtcars)
Use dbListTables()
to view existing tables, it should return db_mtcars
r
dbListTables(con)
Use dbGetQuery()
to pass a SQL query to the database
r
dbGetQuery(con, "select * from db_mtcars")
Close the database connection using dbDisconnect()
r
dbDisconnect(con)
Understand how certain arguments in dbWriteTable()
work
Use dbConnect()
to open a Database connection again
r
con <- dbConnect(RSQLite::SQLite(), "mydatabase.sqlite")
Use dbWriteTable()
to re-create the db_mtcars table using mtcars
data
r
dbWriteTable(con, "db_mtcars", mtcars)
## Error: Table db_mtcars exists in database, and both overwrite and append are FALSE
Use the append
argument in dbWriteTable()
to add the data in the db_mtcars table
r
dbWriteTable(con, "db_mtcars", mtcars, append = TRUE)
Using dbGetQuery()
, check the current record count of db_mtcars, pass the following query: "select count() from db_mtcars"
r
dbGetQuery(con, "select count() from db_mtcars")
Use the overwrite
argument to dbWriteTable()
to replace the data in the db_mtcars table
r
dbWriteTable(con, "db_mtcars", mtcars, overwrite = TRUE)
Check the record count of db_mtcars
again
r
dbGetQuery(con, "select count() from db_mtcars")
Understand how to use dbSendStatement()
to modify the database
Use dbSendStatement()
to pass a SQL commands that deletes any automatic car form db_mtcars
: "delete from db_mtcars where am = 1". Load the command to a variable called rs
r
rs <- dbSendStatement(con, "delete from db_mtcars where am = 1")
Call the rs
variable to view information about the results of the requeted change
r
rs
Use dbHasCompleted()
to confirm that the job is complete
r
dbHasCompleted(rs)
Use dbGetRowsAffected()
to see the number of rows that were affected by the request
r
dbGetRowsAffected(rs)
Clear the results using dbClearResult()
r
dbClearResult(rs)
Confirm that the result set has been removed by calling the rs
variable once more
r
rs
Check the record count of db_mtcars
again, the new count should be 19 (32 original records - 13 deleted records)
r
dbGetQuery(con, "select count() from db_mtcars")
knitr
SQL engineSee how to run SQL queries as code chunks
sql
instead of r
as the first argument of the chunk. Also add connection=con
as another argument of the chunk. `r ''````{sql, connection=con} select * from db_mtcars ```
```{sql, connection=con, echo = FALSE} select * from db_mtcars
2. Add the `max.print` options to the chunk, and set it to 5 ````markdown `r ''````{sql, connection=con, max.print = 5} select * from db_mtcars ``` ```` ```{sql, connection=con, echo = FALSE, max.print = 5} select * from db_mtcars
sql
chunks by using the knitr::opts_chunk$set()
commandknitr::opts_chunk$set(connection = "con", max.print = 5)
sql
chunk, but without any other argument`r ''````{sql} select * from db_mtcars ```
```{sql, echo = FALSE} select * from db_mtcars
5. Close the database connection using `dbDisconnect()` ```r dbDisconnect(con)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.