Remember to
- download
portal_mammals.sqlite.- open
portal_mammals.sqlitein SQLite Manager.- make sure the copy you are going to use in class does not have the
SpeciesCountstable or view.
- We’ve already seen how to connect to databases using
dplyr. - What if we want to work outside of a
dplyrcontext? - Use
DBIandRSQLite.- also other database management systems plugins
Connect
library(DBI)
library(RSQLite)
portalDB <- "portal_mammals.sqlite"
conn <- dbConnect(SQLite(), portalDB)
Check out database structure
dbListTables(conn)
dbListFields(conn, "plots")
dbListFields(conn, "surveys")
Run queries
query <- "SELECT genus, species, COUNT(*)
FROM surveys JOIN species
ON surveys.species_ID = species.species_ID
GROUP BY genus, species;"
species_counts <- dbGetQuery(conn, query)
Write new information to database
Show the original
portal_mammals.sqlitein SQLite Manager.
- Write as a table
dbWriteTable(conn, "SpeciesCounts", species_counts)
dbListTables(conn)
Show
SpeciesCountstable inportal_mammals.sqlite.
- Write as a view
viewquery <- paste("CREATE VIEW SpeciesCounts AS", query)
dbSendQuery(conn, viewquery)
Show the
SpeciesCountsview inportal_mammals.sqlite.
Assign remaining exercises.
